38

I have the following script:

if( $timeout -ne $null )
{
    & $var$timeout 2>&1 > $logDir\$logName
}
else
{
    & $var2>&1 >  $logDir\$logName
}

I'm curious about what 2>&1 is; or, what it represents. I don't know what it's called, otherwise, I'd look it up.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156

2 Answers2

42

It redirects standard error (the 2) to the same place as standard output (the 1)

Sean
  • 60,939
  • 11
  • 97
  • 136
33

The docs are your friends. From PS> man about_Redirection

The Windows PowerShell redirection operators are as follows.

Operator  Description                Example
--------  ----------------------     ------------------------------
<snip>

2>&1      Sends errors (2) and       Get-Process none, Powershell 2>&1
          success output (1)
          to the success
          output stream.

<snip>

The syntax of the redirection operators is as follows:

   <input> <operator> [<path>\]<file>

If the specified file already exists, the redirection operators that do not
append data (> and n>) overwrite the current contents of the file without
warning. However, if the file is a read-only, hidden, or system file, the
redirection fails. The append redirection operators (>> and n>>) do not
write to a read-only file, but they append content to a system or hidden
file.
latkin
  • 16,402
  • 1
  • 47
  • 62
  • 15
    Thank you for the answer, but as I said in my post, how can I search for something if I don't know what it is. If I knew it was redirection, I'd be able to use your answer, and wouldn't have needed to post. – BlackHatSamurai Jul 29 '13 at 20:13
  • 24
    You can search the help system with wildcards. `Get-Help '*&1*'` points to the above topic. – latkin Jul 29 '13 at 20:32