I am just learning PowerShell and need a little guidance.
What I am trying to do is to search a directory recursively for files
that contain a text string and modify the file(s) by replacing that
string with other text.
To do the search, I can use:
dir -r c:\temp\* | Select-string "xyz"
but from there, I am not sure how to go about doing the replace in
each file found.
Can anyone point me in the right direction to accomplish this?
Thanks,
-e
That looks interesting, I was still thinking of a solution using the
Win32 Unix tools find, egrep and sed, bur their sed implementation
didn't update files in place so it all fell a bit flat..
I'm a little puzzled by where -match and -replace come from, as they
are not native to powershell and do not appear to be members of the
string class either. Do they belong to an object - I can't find the
documentation in MSDN 2005?
Thanks,
Duncan.
they asre native powershell operators, for more information see :
Get-Help about_operator
Greetings /\/\o\/\/
Thanks Keith -- I will give this a shot!
-e
Thanks Keith!
I think this function takes what you wrote and expresses it in an easy
to reuse way (if not a little harder to read):
function Replace-String($find, $replace, $includes)
{
get-childitem $includes | select-string $find -list |% { (get-
content $_.Path) |% { $_ -replace $find, $replace } | set-content
$_.Path }
}
http://www.aaronlerch.com/blog/2007/03/powershell-replace-string-function.html