PowerShell has several redirection operators that can be used to redirect specific type of output to a file. For example, if you want to redirect all errors produced by different cmdlet in your script to a text file, you can use Error redirection to do so.
It is particularly helpful when you want to hide errors from appearing on the screen but need them to identify any possible issues and improving the script. Sometimes, when you have a script deployed in production, you can ask users (who are using your script) for the error redirection file to troubleshoot the script.
To send errors to specified file, use 2> file.name
To append errors to a specified file, use 2>> file.name
Following examples attempts to delete files from c:\temp folder and redirects any errors to c:\errors.txt file.
PS C:\> Get-ChildItem -Path c:\temp Directory: C:\temp Mode LastWriteTime Length Name ---- ------------- ------ ---- -a--- 4/24/2016 2:45 PM 972 errors.txt -a--- 4/24/2016 2:48 PM 21085 ex2.docx -a--- 4/24/2016 2:49 PM 274 test.txt -a--- 4/24/2016 2:47 PM 8008 test.xlsx -a--- 4/24/2016 2:49 PM 289 test2.ps1 PS C:\> Get-ChildItem -Path C:\temp | Remove-Item 2> C:\errors.txt PS C:\>
If above command has not resulted in any error, you will not see c:\errors.txt or anything in the file. if the command produced any errors, it will be there in the error file. In example above, two documents were open and they were not deleted. notice how errors were not shown on the console but they were redirected to given error file:
PS C:\> get-content C:\errors.txt Remove-Item : Cannot remove item C:\temp\ex2.docx: The process cannot access the file 'C:\temp\ex2.docx' because it is being used by another process. At line:1 char:31 + Get-ChildItem -Path C:\temp | Remove-Item 2> c:\errors.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\temp\ex2.docx:FileInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand Remove-Item : Cannot remove item C:\temp\test.xlsx: The process cannot access the file 'C:\temp\test.xlsx' because it is being used by another process. At line:1 char:31 + Get-ChildItem -Path C:\temp | Remove-Item 2> c:\errors.txt + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (C:\temp\test.xlsx:FileInfo) [Remove-Item], IOException + FullyQualifiedErrorId : RemoveFileSystemItemIOError,Microsoft.PowerShell.Commands.RemoveItemCommand PS C:\> Get-ChildItem C:\temp
Notice that the error message redirected to the file is same as you would see on the console when not handling (by means of -ErrorAction, Try/Catch etc.). It shows line number that has thrown the error and helps in troubleshooting the script/code.
2> c:\errors.txt in Remove-Item cmdlet in the pipeline is used to direct the errors to given text file (here c:\errors.txt). If you need to append to a file instead of overwriting/creating a new one (2> c:\errors.txt creates the file if does not exist or overwrites it if it exist), use append operator (2>> c:\errors.txt):
PS C:\> Get-ChildItem -Path C:\temp | Remove-Item 2>> c:\errors.txt
One important point to note is, you shouldn’t always be just redirecting errors. There are times when you need to handle errors in a script and take appropriate action. In those cases, its better to use error handling (using try/catch, -ErrorAction parameter of supported cmdlets etc.) to handle the error appropriately.