Using -replace on pipes in powershell

catfind and replacepipepowershell

I want to test out a replace before I use it, so I'm trying to write a quick online command to see what the output is. However, I'm not sure what the syntax is. What I want to do is something like

cat file | -replace "a", "b"

What is the correct powershell syntax for this?

I know that I can also do $a = cat file and then do a replace on $a, but I'de like to keep this on one line

Best Answer

This should do the trick, it'll go through all the lines in the file, and replace any "a" with "b", but you'll need to save that back into a file afterwards

cat file | % {$_.replace("a","b")} | out-file newfile
Related Question