• Skip to primary navigation
  • Skip to main content
  • Skip to footer

Stephanos Constantinou Blog

PowerShell Scripting

  • Home
  • Blogs
    • PowerShell Tutorials
    • PowerShell Scripts
    • PowerShell Modules
      • Modules Cmdlets
    • Software Reviews
  • About
  • Contact
You are here: Home / PowerShell Tutorials / PowerShell Redirection Operators

PowerShell Redirection Operators

11/06/2018 by Stephanos 2 Comments

PowerShell Redirection Operators

In this tutorial we will go through PowerShell Redirection Operators. PowerShell by default sends the output of the commands, warnings and errors in the console. You are able to send those outputs to files in order to store them. There are different ways to redirect the output to files:

  • Out-File
  • Tee-Object
  • Set-Content
  • Add-Content
  • Redirection Operators

We will not go though the commands in this tutorial. We will only check the redirection operators.

PowerShell redirection operators are using specific characters to specify the output to files Please check the list below:

  • * – All output
  • 1 – Success output
  • 2 – Errors
  • 3 – Warnings messages
  • 4 – Verbose Output
  • 5  – Debug messages
  • 6  – Informational messages

In order to use * , 3 , 4 , 5 you need to have PowerShell 3.0 or above. Those four types were introduced in Powershell 3.0. The newer type that has been introduced in PowerShell 5.0 is 6. You need to have PowerShell 5.0 or above in order to used it.

Now we will go through the list of the redirection operators and see some examples.

[adinserter name=”In Article”]

Redirection Operators

  • > – Sends output to the specified file
  • >> – Appends output to the content of the specified file
  • 2> – Sends errors to the specified file.
  • 2>> – Appends errors to the content of the specified file
  • 2>&1 – Sends errors and success output to the success output stream
  • 3> – Sends warnings to the specified file
  • 3>> – Appends warnings to the contents of the specified file.
  • 3>&1 – Sends warnings and success output to the success output stream
  • 4> – Sends verbose output to the specified file
  • 4>> – Appends verbose output to the contents of the specified file
  • 4>&1 – Sends verbose output and success output to the success output stream
  • 5> – Sends debug messages to the specified file
  • 5>> – Appends debug messages to the contents of the specified file
  • 5>&1 – Sends debug messages and success output to the success output stream
  • 6> – Sends informational messages to a specified file
  • 6>> – Appends informational messages to the contents of a specified file
  • 6>&1 – Sends informational messages and success output to the success output stream.
  • *> – Sends all output types to the specified file
  • *>> – Appends all output types to the contents of the specified file
  • *>&1 – Sends all output types to the success output stream

Example 1

Code:

Get-NetAdapter > C:\Scripts_Output\NetAdapters.txt

After you will run the command above, the output of the command will be redirected to the file specified. Below is the content of the file.

PowerShell Redirection Operators - Normal

Then I run the next command and all the content as you can see at the below output, has been replaced.

Code:

Get-NetAdapter -IncludeHidden > C:\Scripts_Output\NetAdapters.txt

Output:

PowerShell Redirection Operators - Replace

 

[adinserter name=”In Article”]

Example 2:

In this example, we will use the same command and redirect the out put to the same file. The difference in this example is that I have changed the redirection operator. I am using >> as redirection operator and the result is to append the second command to the content of the file and not replace it as example 1.

Code:

Get-NetAdapter > C:\Scripts_Output\NetAdapters.txt
Get-NetAdapter -IncludeHidden >> C:\Scripts_Output\NetAdapters.txt

Output:

PowerShell Redirection Operators - Normal

PowerShell Redirection Operators - Append

 

I have used only the first two redirection operators in my examples to help you understand the difference. I will not go through the full list of redirection operators as this will make the tutorial really long. The best way is to try and play with them in order to understand exactly what each operator is doing.

I hope the tutorial about PowerShell Redirection Operators is helpful.

Please let me know your comments and thoughts.

You feedback is appreciated.

[adinserter name=”In Article”]

Related Links:

  • PowerShell Tutorials
  • PowerShell Scripts
  • about_Redirection | Microsoft Docs
  • Out-File – Microsoft Docs
  • Tee-Object – Microsoft Docs
  • Set-Content – Microsoft Docs
  • Add-Content – Microsoft Docs

[adinserter name=”Matched-Content”]

Summary
PowerShell Redirection Operators
Article Name
PowerShell Redirection Operators
Description
PowerShell Redirection Operators. In this tutorial you will find information for redirection operators and how you are able to use them.
Author
Stephanos
Publisher Name
Stephanos Constantinou Blog
Publisher Logo
Stephanos Constantinou Blog

Filed Under: PowerShell Tutorials Tagged With: PowerShell Operators

Reader Interactions

Comments

  1. Mike Shepard says

    11/06/2018 at 17:25

    Looks like you left out stream #6, the Information stream (added in 5.0)

    Reply
    • Stephanos says

      11/06/2018 at 17:58

      Hi Mike,

      Thank you for the notice. I have updated the post and included stream #6 also.

      Thanks
      Stephanos

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Footer

Recent Posts

  • ICS Cube Product Review 26/04/2019
  • PowerShell Module SysInfo v1.2.0 15/03/2019
  • PowerShell Module SysInfo v1.1.2 13/11/2018
  • PowerShell Module SysInfo 24/10/2018
  • Get-VoltageProbe 24/10/2018
  • Get-VideoController 24/10/2018
  • Get-USBController 24/10/2018
  • Get-TrackPoint 24/10/2018
  • Get-TrackBall 24/10/2018
  • Get-TouchScreen 24/10/2018
Planet PowerShell

Categories

  • Modules Cmdlets (57)
  • PowerShell Modules (5)
  • PowerShell Scripts (38)
  • PowerShell Tutorials (35)
  • Software Reviews (2)

Archives

  • April 2019 (1)
  • March 2019 (1)
  • November 2018 (1)
  • October 2018 (56)
  • September 2018 (13)
  • August 2018 (9)
  • July 2018 (6)
  • June 2018 (8)
  • May 2018 (7)
  • April 2018 (9)
  • March 2018 (4)
  • February 2018 (6)
  • January 2018 (12)
  • December 2017 (4)
Top 10 PowerShell 2018

Blogroll

  • Planet PowerShell
  • Reddit – PowerShell
  • PowerShell Magazine
  • PowerShell.org
  • PowerShell Team Blog
  • Hey, Scripting Guy! Blog
  • Mike F Robbins
  • PowerShell Explained with Kevin Marquette
  • Mike Kanakos – Network Admin
  • The Lonely Administrator
  • AskME4Tech
PowerShell Blogs Sysadmin Blogs Banners for Top 20 Programming Blogs

© 2023 · Stephanos Constantinou Blog

  • Home
  • Blogs
  • About
  • Contact