Out-String

Send objects to the host as a series of strings.

Syntax
      Out-String [-inputObject psobject] [-width int]
            [-stream] [CommonParameters]
Key
   -inputObject psobject
       The object to be written to file. A variable, command or expression that gets the objects.
       Use for flat collections, nested collections may need to be unwrapped and passed through the pipe.
       {may also be piped}

   -width int
       The number of characters in each line of output. Any additional characters are truncated, not wrapped.
       The default is determined by the host, for PowerShell.exe  this is 80 (characters).

   -stream 
       Send the strings for each object separately. 
       By default, the strings for each object are accumulated
       and sent as a single string.

Out-String enables searching and manipulating string output as you would in traditional shells where object manipulation is less convenient than in PowerShell.

The final part of displaying PowerShell output is a hidden background call to an Output cmdlet, by default as the last part of the execution process PowerShell calls the default output cmdlet which is typically Out-Host.

Base64

Converting to base64, allows representing a unicode string as an ASCII compatible string (for obfuscation or ease of export). There is no built in cmdlet for this but we can call the a .Net method ToBase64String:

$text = 'Hello World' 
$result = [Convert]::ToBase64String([System.Text.Encoding]::Unicode.GetBytes($text), 'InsertLineBreaks')
$result

# To reverse the above:
$base64 = 'SABlAGwAbABvACAAVwBvAHIAbABkAA=='
$text = [Text.Encoding]::Unicode.GetString([Convert]::FromBase64String($base64))
$text

Examples

Send the content of Test1.txt to the console as a single string:

PS C:\> get-content C:\docs\test1.txt | out-string

Get the regional settings for the current user and convert the data to strings:

PS C:\> $cult = get-culture | select-object *
out-string -inputobject $cult -width 100

Display all aliases that include the phrase "Get-"

PS C:\> get-alias | out-string -stream | select-string "Get-"

Piping into Select-String demonstrates the difference between working with objects and working with strings. The -stream parameter sends each string individually, as without this Select-String would match and return the entire single string containing all of the aliases.

#Brown paper packages tied up with strings, these are a few of my favorite things# ~ Oscar Hammerstein III

Related PowerShell Cmdlets

Export-CSV - Export a PowerShell object to a comma-separated values (CSV) file.
Out-Default - Send output to default.
Out-File - Send command output to a file.
Out-GridView - Send output to an interactive table.
Out-Host - Send the pipelined output to the host.
Out-Null - Send output to null.
Out-Printer - Send the output to a printer.
Tee-Object - Send input objects to two places.
Out-Notepad - Function to send text direct to Notepad.exe by calling user32.dll
Equivalent bash command: redirection - Redirection and Process Substitution.


 
Copyright © 1999-2024 SS64.com
Some rights reserved