A while ago i wanted to update a connection string in a configuration file. My first attempt was the following:

Get-Content $File | Foreach {$_ -Replace "Source>(.*?)<", "Source>$New<" } | Set-Content$File;


Running this scripts leads to the following error: “Set-Content : The process cannot access the file because it is being used by another process.” In order to avoid this you can complete the read operation before you start writing as following:

(Get-Content $File) | Foreach {$_ -Replace "Source>(.*?)<", "Source>$New<" } | Set-Content$File;