I have a file with the same text occurring 5 times in total. I now want to only replace the first 2 occurrences with a powershell command within a batch file.
Hello -- I want to only
Hello -- replace those.
Hello
Hello
Hello
So my file will look like that:
Goodbye
Goodbye
Hello
Hello
Hello
My code looks like that:
powershell -command "$null=new-item -force test.txt -value ((gc -encoding utf8 test.txt) -replace ('^.*$','Goodbye') | out-string)"
I tried using the answer to Replacing only the first occurrence of a word in a string:
powershell -command "$null=new-item -force test.txt -value ((gc -encoding utf8 test.txt) -replace ('^.*$','Goodbye',2) | out-string)"
Which gave me the BadReplaceArgument error.