Your problem here is that Get-Content
returns a string[]
(with one item for each line in the source file) while [regex]::Replace
expects a string. That's why the array will first be converted to a string which simply means lumping together all items.
PowerShell provides a -replace
operator which will handle this case more gracefully:
(Get-Content .extfile.txt) -replace 'line', 'line2' |
out-file -encoding ascii textfile2.txt
The -replace
operator operates on each item of an array individually i it's applied to an array.
And yes, it does regular expression matches and replaces. For example:
> (Get-Content .extfile.txt) -replace '(i|o|u)', '$1$1'
liinee
liinee
Soomee liinee
Mooree liinee heeree
Read a bit more here and here.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…