Out-String in PowerShell: Convert Output to Text

If you need PowerShell output as plain text for a ticket, an email or a log, Out-String is what you need. It turns any output into exactly the text you’d see on screen:

$text = $orders | Out-String

In this tutorial, I’ll show you how PowerShell Out-String works: one string vs -Stream, the trailing newline, -Width, -NoNewline, searching formatted output and what changes between PowerShell 7 and 5.1.

What does Out-String do in PowerShell?

Out-String sends objects through the same formatter the console uses, then returns the result as one System.String instead of printing it:

$orders = @(
    [pscustomobject]@{ Customer = 'Maria Lopez'; City = 'Austin'; Total = 1250 }
    [pscustomobject]@{ Customer = 'Tom Baker'; City = 'Denver'; Total = 480 }
    [pscustomobject]@{ Customer = 'Dana Reyes'; City = 'Seattle'; Total = 2100 }
)

$text = $orders | Out-String
$text
"Type: $($text.GetType().Name)"
"Strings returned: $(@($text).Count)"

Output:

Customer    City    Total
--------    ----    -----
Maria Lopez Austin   1250
Tom Baker   Denver    480
Dana Reyes  Seattle  2100


Type: String
Strings returned: 1
PowerShell 7 console showing Out-String turning a table of orders into one String
Out-String returned the whole table as a single String (PowerShell 7)

The headers and spacing are now baked into text, so $text.Total no longer works. That’s the main difference from other ways to convert an object to a string.

Out-String vs Out-String -Stream

Add -Stream and Out-String sends one string per line instead:

$orders = @(
    [pscustomobject]@{ Customer = 'Maria Lopez'; City = 'Austin'; Total = 1250 }
    [pscustomobject]@{ Customer = 'Tom Baker'; City = 'Denver'; Total = 480 }
    [pscustomobject]@{ Customer = 'Dana Reyes'; City = 'Seattle'; Total = 2100 }
)

$lines = $orders | Out-String -Stream
"Lines: $($lines.Count)"
$lines | ForEach-Object { "[$_]" }

Output:

Lines: 7
[]
[Customer    City    Total]
[--------    ----    -----]
[Maria Lopez Austin   1250]
[Tom Baker   Denver    480]
[Dana Reyes  Seattle  2100]
[]
Windows PowerShell 5.1 console showing Out-String -Stream returning 8 lines
-Stream returned 8 lines, one more blank line than PowerShell 7 (Windows PowerShell 5.1)

The output above is from PowerShell 7: 7 lines, with a blank one at each end. Windows PowerShell 5.1 returned 8. Both versions also have oss, a built-in shortcut function for Out-String -Stream.

Why does Out-String add blank lines? Trim the trailing newline

Out-String keeps the blank line before a table and the line breaks after it, which breaks string comparisons:

# $orders is the same 3-order array from the first example

$text = $orders | Out-String
"Before Trim: $($text.Length) characters"
$clean = $text.Trim()
"After Trim: $($clean.Length) characters"
"Starts with the header: $($clean.StartsWith('Customer'))"

Output:

Before Trim: 139 characters
After Trim: 133 characters
Starts with the header: True

The output above is from PowerShell 7. Windows PowerShell 5.1 counted 141 characters before the trim, because it adds one more line break. After .Trim(), both returned 133.

Trim before you compare, log or save. The other trim methods handle one-sided cleanup.