In place replace using powershell In place replace using powershell unix unix

In place replace using powershell


Maybe it would help to get back to your original goal of implementing the equivalent of the Unix version. Here is essentially the equivalent PowerShell version.

$search = '<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>'$replace = 'test'$dir = 'D:\code\cpp\FileHandlingCpp\input - Copy'dir -Path $dir -Recurse -Filter *.xml | ForEach-Object {    (Get-Content -Path $_.FullName) -replace $search, $replace |         Set-Content $_.FullName}

Note - watch out for text file encoding changes that may occur from re-writing the file. You can specify the output encoding if you need to using Set-Content's -Encoding parameter e.g. ASCII.


This took me a while to figure out but I got it!

It's a one liner. Just go to the folder you want to start at and type this in. Change the file.name (use wild cards if you want) and string1 and string2 with the file name you want to search for and the string1 you want to replace with string2.

So this searches folders recursivly and for each file it replaces a string with another string and saves it. Basically Get-Childitem | ForEach-Object Replace and Save.

All set!

get-childitem -include file.name -Recurse | ForEach-Object { ( Get-Content -Path $_.FullName ) -replace 'string1', 'string2' | set-content $_.fullname }