Redirecting output streams in Powershell

Found this on MS connect site discussion and reposting here because I don’t know about you but I can never remember the syntax

Posted by Microsoft on 15.2.2012 at 16:40

 

# In PS 3.0, we've extended output redirection to include the following streams:
# Pipeline (1)
# Error    (2)
# Warning (3)
# Verbose (4)
# Debug    (5)
# All     (*)

# We still use the same operators
# >    Redirect to a file and replace contents
# >>    Redirect to a file and append to existing content
# >&1    Merge with pipeline output

# First, let's set some preference variables and get a temp file
$VerbosePreference = "Continue"; $DebugPreference = "Continue"; $filename = [System.IO.Path]::GetTempFileName()

# Scenario 1
# Merge warning output with pipeline output (3>&1)
# Pipeline output is assigned to a variable
# The error output goes to the host (no redirection)
$var = $(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Error "Error Output") 3>&1
# The variable $var contains the pipeline and warning output
$var

# Scenario 2
# Redirect warning output to a file (3>$filename)
# All other output is merged with pipeline output (2>&1 4>&1 5>&1)
$var = $(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output") 2>&1 4>&1 5>&1 3>$filename
# Pipeline, Verbose, and Error output are now in $var
$var
# Warning output is in the file
Get-Content $filename

# Scenario 3
# Redirect all output to a file (*>$filename)
$(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output"; Write-Debug "Debug Output") *>$filename
# All output is now in the file
Get-Content $filename

# Scenario 4
# Merge warning, error, and verbose output with the pipeline output (2>&1 3>&1 4>&1)
# Redirect the pipeline output to a file (1>$filename)
# Debug output goes to the host (no redirection)
$(Write-Output "Pipeline Output"; Write-Warning "Warning Output"; Write-Verbose "Verbose Output"; Write-Error "Error Output"; Write-Debug "Debug Output") 2>&1 3>&1 4>&1 1>$filename
# All other output is now in the file
Get-Content $filename 

One recent example from me:

# redirect all but debug stream into a file
# Sharepoint 2013 install - keeping for future reference
Initialize-SPResourceSecurity -Verbose 2>&1 3>&1 4>&1 1> C:\Install\install_logs\Initialize-SPResourceSecurity.txt
Advertisements

Tagged:

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

smsagent

Tips, tricks and time-savers for the Windows and ConfigMgr administrator

To The Point

Anything about Technology and Business

Brian's Power Windows Blog

Microsoft in the Enterprise. Windows, Hyper-V, Exchange, SQL, and more!

PowerScripting Podcast

Shownotes and links for the PowerScripting Podcast, a podcast to help people learn Windows Powershell

Learn Powershell | Achieve More

What is this Powershell of which you speak?

%d bloggers like this: