Welcome PowerShell User! This recipe is just one of the hundreds of useful resources contained in the PowerShell Cookbook.
If you own the book already, login here to get free, online, searchable access to the entire book's content.
If not, the Windows PowerShell Cookbook is available at Amazon, or any of your other favourite book retailers. If you want to see what the PowerShell Cookbook has to offer, enjoy this free 90 page e-book sample: "The Windows PowerShell Interactive Shell".
There are several ways to capture the output of commands in PowerShell, as listed in Table A-16.
Command | Result |
---|---|
|
Stores the objects produced by the PowerShell command into |
|
Stores the visual representation of the PowerShell command into |
|
Stores the (string) output of the native command into |
|
For most commands, stores the objects produced by the PowerShell command into |
|
Redirects the visual representation of the PowerShell (or standard output of a native command) into |
|
Redirects the visual representation of the PowerShell (or standard output of a native command) into |
|
Redirects the errors from the PowerShell or native command into |
|
Redirects stream number |
|
Redirects the errors from the PowerShell or native command into |
|
Redirects stream number |
|
Redirects both the error and standard output streams of the PowerShell or native command into |
|
Redirects both the error and standard output streams of the PowerShell or native command into |
While output from the Write-Host
cmdlet normally goes directly to the screen, you can use the structured information stream to capture it into a variable:
PS > function HostWriter { Write-Host "Console Output" } PS > $a = HostWriter Console Output PS > $a PS > $a = HostWriter 6>&1 PS > $a Console Output