Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace path in text file with Powershell

Tags:

powershell

I have a problem with replace text in Powershell. For e.g.

When I use:

(Get-Content C:\TEMP\App.config) -replace "one","two" | Set-Content C:\TEMP\App.config

it works. But when I use:

(Get-Content C:\TEMP\App.config) -replace "C:\Program Files (x86)\Adobe\Acrobat Reader 10\Reader\AcroRd32.exe","C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe" | Set-Content C:\TEMP\App.config

is doesn't work. I search a lot of info in Google or documentation but still have a problem.

Can anybody help? :-)

like image 912
tkobus Avatar asked Jun 17 '26 06:06

tkobus


1 Answers

You should be aware that the -replace command uses regex. It should work, if you escape the replace string:

$searchString = [Regex]::Escape('C:\Program Files (x86)\Adobe\Acrobat Reader 10\Reader\AcroRd32.exe')
$replaceString = 'C:\Program Files (x86)\Adobe\Acrobat Reader DC\Reader\AcroRd32.exe'

(Get-Content C:\TEMP\App.config) -replace $searchString, $replaceString | Set-Content C:\TEMP\App.config
like image 161
Martin Brandl Avatar answered Jun 19 '26 07:06

Martin Brandl