By default, Windows PowerShell sends output
to the console. However, you can redirect the output to a file by using
the redirection operators, which are listed in Table 1.
Table 1. Windows PowerShell Redirection Operators
Operator | Description |
---|
> | Sends the output to a file |
>> | Appends the output to a file |
2> | Sends errors to a file |
2>> | Appends errors to a file |
2>&1 | Sends errors to the success output stream |
To send the output of a cmdlet to a file, use the > operator. The following example redirects the output of the Get-Command cmdlet to a file named CommandList.txt, and overwrites the file if it exists.
PS > Get-Command > CommandList.txt
To append content to a file instead of replacing it, use the >> operator:
PS > Get-Command >> CommandList.txt
The 2> operator redirects all errors
that occurred. The following command redirects all errors that occur to
a file instead of displaying the error in the PowerShell console,
overwriting the file if it already exists.
PS > Get-ChildItem C:\nofile.txt 2> Errors.txt
To append the errors to the file instead, use the 2>> operator:
PS > Get-ChildItem C:\nofile.txt 2>> Errors.txt
Note
You can also redirect output to a file using cmdlets that handle redirection, such as Out-File.