amateur amateur - 1 year ago 122
PowerShell Question

How can I replace every occurence of a String in a file with PowerShell?

Using PowerShell, I want to replace all exact occurrences of

in a given file with
. What is the easiest way to do so?

Answer Source

Use (V3 version):

(Get-Content c:\temp\test.txt).replace('[MYID]', 'MyValue') | Set-Content c:\temp\test.txt

Or for V2:

(Get-Content c:\temp\test.txt) -replace '\[MYID\]', 'MyValue' | Set-Content c:\temp\test.txt
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download