amateur amateur - 4 months ago 10
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

[MYID]
in a given file with
MyValue
. What is the easiest way to do so?

Answer

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