I\'m trying to write a simple script that reads a file, locates a string, replaces the string with another string, and stores all new file contents (with replaced string), i
The problem is the second element in your pipeline.
{$_ -replace "this:text", "withthis:text"}
This is a scriptblock (i.e. a piece of code). If you want to apply a scriptblock to all of the incoming items on a pipeline you can use the foreach-object
cmdlet like this:
(Get-Content C:\file1.txt) | foreach-object {$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt
@shagun is using the %
alias for the foreach-object
cmdlet, so that code looks correct as well.