PowerShell – Replace a line of text with quotes in multiple files

This morning I found myself in a situation where I would need to change one simple line/word in about 150 files.
I could have done this with notepad… but doing it 150 times? I really don’t think so!
Yes, there are tools for this… but where’s the fun in a tool when you can do it with PowerShell?

Get-ChildItem C:\vApps *.osd -recurse | Foreach-Object { $c = ($_ | Get-Content) $c = $c -replace ‘<SCRIPT TIMING="PRE"/’,'<SCRIPT TIMING="POST"/’ [IO.File]::WriteAllText($_.FullName, ($c -join "rn")) }

This script will recursively replace “<SCRIPT TIMING="PRE"” with “<SCRIPT TIMING="POST"” in all files with the OSD extension in the folder C:\vApps.

Leave a Reply

Your email address will not be published. Required fields are marked *