Powershell Config editing help

Mak

Limp Gawd
Joined
Oct 14, 2005
Messages
150
I'm trying to automate as much as possible at work, and in turn, learn powershell. I just started, so I am struggling a little bit with the following scenario:

I have 1600 config files in ~30 or so sub-dirs. I'd like something that could replace all of a given value with a new value. The problem I'm having is that I cannot figure out how to recursively move between sub-dirs and have the script work. In the meantime, I "hardcoded" the 'cd's to move through the directories, but this is far too clunky to use everywhere.

I appreciate any help.

Code:
$old = Read-Host 'What is the value you are looking to replace?'
$new = Read-Host 'What is the new value you want?'

cd 'C:\Path\To\Scripts'
$biotas = Get-ChildItem

ForEach ($biota in $biotas) {
    (Get-Content $biota) | ForEach-Object { $_ -replace "$old","$new"} | Set-Content $biota
    }
 
The solution ended up being rather simple. Someone at the technet forums was able to help. I'm posting this here just in case anyone else searches for a solution....

Code:
Get-ChildItem "C:\somedir" -recurse -include *.cfg | select-object -expand fullname | 
foreach {(Get-Content $_) -replace $old,$new | Set-Content $_}
 
Be careful with this, with great power comes great responsibility!

It is a beautiful language though, concise yet powerful.
 
Back
Top