PowerShell – UNIX SED Equivalent – Change Text In File

Unix SED command is very useful to make changes to a configuration file when you know what the default values are, below is a way to do a SED-like text change in PowerShell

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

Example: TeamCity Build Agent Configuration file needs updated so it knows where the server is.

$TeamCityConfPath = 'C:\TeamCity\buildAgent\conf\buildAgent.properties'
(Get-Content $TeamCityConfPath).replace('serverUrl=http://localhost:8111/', 'serverUrl=http://someserver:1234/') | Set-Content $TeamCityConfPath
(Get-Content $TeamCityConfPath).replace('name=Default Agent', 'name=TeamCityBA1') | Set-Content $TeamCityConfPath
All information on this site is shared with the intention to help. Before any source code or program is ran on a production (non-development) system it is suggested you test it and fully understand what it is doing not just what it appears it is doing. I accept no responsibility for any damage you may do with this code.