Your text file likely uses CRLF ("`r`n"
) newlines rather than LF-only ("`n"
) newlines, so that replacing just the latter isn't enough.
A simple way to avoid this problem is to not use -Raw
, so that Get-Content
returns an array of lines, which you can the join with |
:
$s_01 = (Get-Content $path\source_files\pascodes.txt) -join '|'
Note that Get-Content
recognizes CRLF and LF newlines interchangeably, as does PowerShell in general.
While you could fix your original attempt as follows, the above solution is much simpler (while it is slower, that won't matter with small files):
$s_01 = (Get-Content $path\source_files\pascodes.txt -Raw).Trim() `
-replace '\r?\n' , '|'
The regex \r?\n
used with the -replace
operator above matches both CRLF and LF newlines.
.Trim()
is applied first in order to remove any trailing newline, so that it doesn't also get replaced with |
; note that this trims all leading and trailing whitespace; to trim just a single trailing newline, use -replace '\r?\n\z'
instead (before applying the -replace
operation above).