Hello and my apologies up front if this has been addressed under a different post. Honestly, I’ve read so much over the past few days my head hurts,
My Goal: Search all files in a given path (including sub-directories) and replace strings with new strings.
For example: Search all files in c:\temp* and d:\temp* for “servername1” and “servername2” and replace those strings with “newservername1” and “newservername2”.
I haven’t convinced myself I want this all in one script as there may be files like Web.configOLD that I would not want to update. That said once I find out what file(s) contain these strings I can then make a decision how to proceed and will eventually want this all in one script.
I can successfully gather the path and file name into a file using the following:
$Date = Get-Date -Format MM_dd_yyyy
$exclude = @(“log”, “bak”)
$SearchString = ‘Server1’, ‘Server2’, ‘Server3’
$FileName = “$Date ServerNamesSearchResults”
Get-ChildItem -Path “D:\temp”, “c:\temp” -Recurse -exclude $exclude | Select-String $SearchString | Select-Object -Unique Path | FT -hide | Out-File “D:\PSScripts\Results$FileName.txt”
This results in a text file that contains something like this:
D:\temp\New Text Document.txt
C:\temp\New Text Document (2).txt
C:\temp\New Text Document.txt
Now the hard part.
I now want to replace the strings “servername1” and “servername2” in each of these files with “newservername1” and “newservername2”
Please help. I’m sure there is even a better way to do this but I haven’t figured it out.