To complement Ansgar Wiechers' helpful answer:
Invoking an external command returns the output lines as an array of strings.
Because Encoding.GetBytes()
expects a single string as input, the array was implicitly coerced to a single string, in which case PowerShell joins the array elements using the space character, so the original line formatting was lost; e.g.:
PS> [string] 1, 2, 3
1 2 3 # single string containing the array elements joined with spaces
Piping to Out-String
, as in Ansgar's answer, prevents creation of the array and returns the external command output as a single string.
PS> (1, 2, 3 | Out-String | Measure-Object).Count
1 # Out-String output a single string
Another option would be to join the array elements with newlines on demand (you won't see the difference in the console, but you do get a single, multi-line output string with this technique):
PS> (net users) -join "`n" # or, more robustly: [environment]::NewLine
Note: With this technique, the output string will not have a trailing newline, unlike when you use Out-String
.
Out-String
always appends a trailing newline, which can be undesired.
Alternatively, you can tell PowerShell what encoding to expect from an external command by setting [Console]::OutputEncoding
(temporarily):
However, that is only necessary if you know the external utility to use an output encoding other than the default (your system's active OEM code page) - and I doubt that that's necessary for net users
; that said, here's how it would work:
$prevEnc = [Console]::OutputEncoding
[Console]::OutputEncoding = New-Object System.Text.UTF8Encoding
$str = net users | Out-String # `net users` output is now properly decoded as UTF-8
[Console]::OutputEncoding = $prevEnc