Due to use of Get-Content
-Raw
, your input string is a multiline string, but the ^
and $
regex anchors by default only operate on the start and end of the string as a whole, not on individual lines.
You have two options:
Include the Multiline
regex option, which makes ^
and $
match the start and end of individual lines instead; its inline form - which must be placed at the start of the regex - is (?m)
:
(Get-Content .\text.txt -Raw) -replace '(?m)^#wsuri=myuri$','bbb'
Alternatively, for (slower) line-by-line input processing, which outputs an array of lines, simply omit -Raw
:
(Get-Content .\text.txt) -replace '^#wsuri=myuri$','bbb'
Note that -replace
, like all PowerShell comparison-like operators, accepts an array as the LHS, in which case the operation is performed on each element, which in this case means that each line from the input file is processed separately.