Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Basic PowerShell Script Issue: "Expressions are only allowed as the first element of a pipeline"

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), in a new file. Here is what I'm using:

(Get-Content C:\file1.txt) | {$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt

The error I'm receiving is: "Expressions are only allowed as the first element of a pipeline"

I'm pretty sure this is because of the colon ":" character being in both the string I'm locating and replacing it with. I've tried escaping the colon character with "\" and "`" characters, but I'm receiving the same errors. Does anyone know what's wrong with this?

Thanks for the help.

like image 717
Jay Parken Avatar asked Aug 01 '14 10:08

Jay Parken


1 Answers

I guess it is because after first pipe you are not processing each result. so the right one will be according to me :

(Get-Content C:\file1.txt) | %{$_ -replace "this:text", "withthis:text"} | Set-Content C:\file2.txt
like image 168
Shagun Sharma Tamta Avatar answered Sep 23 '22 23:09

Shagun Sharma Tamta