Powershell: Recursively Replace String in Select Sub-files of a Directory
try:
gci -r -include "*.bat","*.config","*.cfg" | foreach-object { $a = $_.fullname; ( get-content $a ) | foreach-object { $_ -replace "D:","C:\path" } | set-content $a }
If you want to use the above code on ALL FILES use this code, note the top line:
gci -r *.* |foreach-object { $a = $_.fullname; ( get-content $a ) |foreach-object { $_ -replace "master81","master" } | set-content $a }
if you leave out *.*
you may get errors because you are trying to edit folders.
[Get-Content], UnauthorizedAccessException
I recognized that I needed an extra variable. One way to do this is by integrating a for-loop into the outer portion of the command. I used:
foreach ($f in gci -r -include "*.bat") { (gc $f.fullname) | foreach { $_ -replace "D:","C:\path" } | sc $f.fullname }