Replacing All Instance of String in Folder and SubFolders

Hello everybody,

I have a web based application that our customers use and I’m trying to create a script to migrate the configuration files from one server to a new server. So far I’ve been able to install the application onto the new server with a clean installation and copy the configuration files over from the old server. The last thing I need to do is replace all instances of the hostname, the IP address, and the file path (assuming it’s on a different drive letter than the original server. The configuration files are all in on main directory folder but it contains about 70 sub folders. There are also many different file types involved (.xml, .config, .txt). I need help in figuring out how to get it to search all the sub folders. I’ve managed to figure out how to replace one variable at a time, but it’s only changing it into the main directory, not in the subfolders. Any suggestions on how to get this to search and replace in the subfolders? Any suggestions on how to get this to replace all three variables (if they exist in that file) at the same time, as opposed to searching the entire directory three times? This is what I’ve come up with so far:

$InputFiles = Get-Item "E:\Test\*.*"
$OldString  = 'OLDHOSTNAME'
$NewString  = 'NEWHOSTNAME'
$InputFiles | ForEach {
    (Get-Content -Path $_.FullName).Replace($OldString,$NewString) | Set-Content -Path $_.FullName

Instead of Get-Item I would recommend to use Get-ChildItem with the parameter -Recurse and I would recommend to limit the search for files to the ones you really need with the parameter -Filter.

Thank you sir! Moderators were slow to get this up yesterday, so I managed to get it to pretty much do what I needed it to.