I am trying to find and replace a strings in a file and then saving it to the original file in PowerShell.
I've tried doing
(Get-Content "C:\Users\anon\Desktop\test.txt")
-replace 'apple', 'apple1'
-replace 'bear' , 'bear1' |
Out-File test1.txt
pause
However, I keep getting
-replace : The term '-replace' is not recognized as the name of a cmdlet, function,
script file, or operable program. Check the spelling of the name, or if a path was
included, verify that the path is correct and try again.
At C:\Users\Xing Chen\Desktop\test.ps1:2 char:1
+ -replace 'apple', 'apple1'
+ ~~~~~~~~
+ CategoryInfo : ObjectNotFound: (-replace:String) [], CommandNotFoundException
+ FullyQualifiedErrorId : CommandNotFoundException
I've been able to use "abcd".replace() fine and according to the documentation -replace should work too.
-replaceoperator in a new line without escaping the line break PowerShell will interpret(Get-Content "C:\Users\anon\Desktop\test.txt")as a complete statement and-replace 'apple', 'apple1'as another statement (with a command it doesn't recognize. Put the-replaceoperations on the same line asGet-Contentand the problem will disappear.