Share via


PowerShell: Input/Output Streams Concepts

All legacy commandline shells like CMD, and Unix shells like Bash, Ksh etc. deal with three streams: stdin, stdout and stderr for Input, Output and Error respectively.

Wikipedia: Standard streams: http://en.wikipedia.org/wiki/Standard_streams

PowerShell has many more: Input, Output, Verbose, Warning, Debug, Progress and Error.

The main purpose of this is to separate different kind of messages from being included within legitimate output.

Stream Types

Input

The Input stream (stdin) gets the Input (User Input) from the console Window with the Cmdlet Read-Host

Verbose

The verbose stream displays only messages written with the Write-Verbose Cmdlet, if a script or a Cmdlet is executed with the –Verbose switch. See Preference Variable $VerbosePreference

Warning

The warning stream uses the Write-Warning Cmdlet to display nonterminating errors. The response to the warning depends on the value of the user's $WarningPreference variable and the use of the WarningAction common parameter.

Error

The error stream (stderr) represents all (terminating) errors. You can write to the error stream with the Cmdlet Write-Error. See Preference Variables $ErrorActionPreference, $ErrorView

Debug

The debug stream is only displayed in the debug mode of the script environment you can user Write-Debug to write to this stream. By default, debug messages are not displayed in the console, but you can display them by using the Debug parameter or the $DebugPreference variable.

Progress

You can write to the progress stream with the Write-Progress Cmdlet. See Preference Variable $ProgressPreference

Output

The output stream (stdout) represents the "normal" output of a command with the Cmdlet Write-Host or better : Out-Default

Preference Variables

There are different Preference Variables who change the behavior of the Cmdlets:

  • $ErrorActionPreference,
  • $ErrorView,
  • $WarningPreference,
  • $DebugPreference,
  • $ProgressPreference,
  • $VerbosePreference

For this topic see in the PowerShell Help!

For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!

Get-Help  about_Preference_Variables –Full –Online

Output

By default, Windows PowerShell sends its command output to the Windows PowerShell console with Out-Default.

Cmdlet Write-*  direct its output to  ---> Stream direct its output to  ---> Out-Default

However, you can direct the output of a stream (not a cmdlet !) to a text file, and you can redirect error stream output to the regular output stream.

In PowerShell 2.0 this was limited to the error and the output stream.

In PowerShell 3.0 you can now redirect all streams except the progress stream!

For this topic see in the PowerShell Help!

For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!

See more

See also

Get Help

For PowerShell 3.0 it is possible you have to use the –online Parameter with the Get-Help cmdlet to get rich Help!

  • Get-Help about_Preference_Variables –Full –Online

  • Get-Help Write-Error –Full -Online

  • Get-Help Write-Host –Full -Online

  • Get-Help Write-Output –Full -Online

  • Get-Help Write-Progress –Full -Online

  • Get-Help Write-Verbose –Full -Online

  • Get-Help Write-Warning –Full –Online

  • Get-Help Out-Host –Full –Online

  • Get-Help Out-Default –Full –Online

  • Get-Help Out-File –Full –Online

  • Get-Help Out-String –Full –Online

  • Get-Help Out-Null –Full –Online

Capture Warning, Verbose, Debug and Host Output via alternate streams

http://connect.microsoft.com/PowerShell/feedback/details/297055/capture-warning-verbose-debug-and-host-output-via-alternate-streams

Verbose or Debug?

http://jdhitsolutions.com/blog/2011/08/verbose-or-debug/

PowerShell Automatic Logging

http://jdhitsolutions.com/blog/2011/03/powershell-automatic-logging/

Other Languages