Replace unknown characters in text file using Powershell script?

812 Views Asked by At

I am writing a powershell script to edit multiple text files. All the text files have a line like so

serverUrl=http://localhost:1234

The last 4 numbers vary in each file, but I need to change them all to 9090. I had tried using powershell wildcards like so:

foreach ($file in $files) {
        (Get-Content $file).replace('serverUrl=http\://localhost\:????', 'serverUrl=http\://localhost\:9090') | Set-Content $file
}

But these didn't work unfortunately. Is there any way to do this? Thank you

1

There are 1 best solutions below

0
Mathias R. Jessen On

Use the -replace regex operator instead of the String.Replace() method:

(Get-Content $file) -replace 'serverUrl=http://localhost:\d{4}','serverUrl=http://localhost:9080'