7

I'm trying replace a few strings within another PowerShell file.

$Source_IP = Read-Host 'Enter source IP'
$Target_IP = Read-Host 'Enter target IP'

By using the following line in another PowerShell script, the file shows as Modified, but the changes don't take effect.

(Get-Content "C:\Solutions.ps1") -replace "$Target_IP = Read-Host 'Enter target IP'", "$Target_IP = '192.168.0.221'" | Set-Content "C:\Solutions.ps1"

Is there a reason why the changes don't take effect?

This is running as an Administrator, on Windows Server 2008, and PowerShell version 2 I believe.

4
  • 3
    1) Double quoted string literals expand variables. 2) -replace use regular expressions. Commented Jan 28, 2016 at 13:13
  • The variables don't exist in the local script, only the remote script, so I don't think expanding could be a problem. Regular Expressions though... perhaps. Anyway to have it treat these are just 2 literal strings to replace? Commented Jan 28, 2016 at 13:40
  • (Get-Content "C:\Solutions.ps1") | ForEach-Object{$_.Replace($Source_IP,$Target_IP)} | Set-Content "C:\Solutions.ps1"? Just simple matching no regex. Commented Jan 28, 2016 at 14:00
  • @Matt That works, (Get-Content "C:\Solutions.ps1") | ForEach-Object{$_.Replace($old_string,$new_string)} | Set-Content "C:\Solutions.ps1" for the first file. When I run the same commands against a 2nd file it doesn't take effect. Maybe there is a limit and I can only do this once per ps1 file? Commented Jan 28, 2016 at 14:20

2 Answers 2

9

As PetSerAl points out the -replace comparison operator supports regex. While you can have some degree of expressions in your patterns you are adding an unnecessary amount of complexity especially since you are just using simple matches anyway.

The easier solution is to use the string method .Replace().

$filePath = "C:\Solutions.ps1"
(Get-Content $filePath).Replace($Source_IP,$Target_IP) | Set-Content $filePath

Note that .Replace() is case sensitive. If you are just replacing IP addresses it is a moot point. I am unsure why you are having issues with a second file.

Sign up to request clarification or add additional context in comments.

2 Comments

May I ask you why the pipe to out-string is needed? Couldn't it just be (Get-Content $filePath).replace(... ?
@POliveira It should not be required or necessary. It must have been something I was testing and forgot to take out. Can't think of a reason why I put that in there. Perhaps an issue with psv2. or something. Either way im taking it out.
0

Just to point out that, as for what stated before, the -replace operator supports regex and in a regex you have to escape special characters like $ in the search string with a \ , moreover you are using double quotes strings so you have to escape before the $ with powershell escape character ` in both search and replace strings, so the command should be (can't try it right now):

(Get-Content "C:\Solutions.ps1") -replace "\`$Target_IP = Read-Host 'Enter target IP'", "`$Target_IP = '192.168.0.221'" | Set-Content "C:\Solutions.ps1"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.