I’m using Powershell for replace a string in a text file.
For replace String 1 to String 2 in file.txt I use
(Get-Content file.ps1) | Foreach-Object { $_ -replace 'String 1', 'String 2'} | Set-Content file.txt
It works pretty well even with for replace characters like – and #
It need to replace a string with the [ character but it doesn’t work. I need to replace -state [VMstate]::stopped with -state stopped but it doesn’t work with
(Get-Content TEMP_config.ps1) | Foreach-Object { $_ -replace '-state [VMstate]::stopped', '-state stopped'} | Set-Content TEMP_config.ps1
How can i find the [ char?
The replace operator uses regular expressions to match text. One way to search for literal text is to escape the search text: