Write stderror to a file using PowerShell

Posted by Zian Choy on Server Fault See other posts from Server Fault or by Zian Choy
Published on 2012-12-01T01:34:59Z Indexed on 2012/12/01 5:06 UTC
Read the original article Hit count: 220

Filed under:
|
|

How do I capture error messages from a PowerShell-launched command in a text file?

I searched the Internet for a while and found that supposedly, I should be able to do something like cmd /c "big blob of text >C:\output.txt 2>c:\errors.txt" to direct the output to output.txt and the errors to errors.txt but when I try to run the command, I get the following error:

cmd.exe : The filename, directory name, or volume label syntax is incorrect.
At C:\Users\Zian\Desktop\Untitled1.ps1:27 char:4
+ cmd <<<<  /c $command 
    + CategoryInfo          : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Furthermore, if I try to run the command without everything starting at "2", then the command executes correctly and output.txt catches the right output.

I looked at Redirect stderr to variable in powershell but it wasn't helpful because the answer to that question suggests capturing the entire output and filtering it in memory. In my case, I am backing up every database on a computer and since the databases won't fit in my laptop's RAM, I cannot use the question's solution.

I also found tantalizing suggestions about using $err = @(command goes here) but with no information on what to do other than simply inserting that line of text. I tried to utilize the search function on Serverfault with the string "@()", but it did not return any results.

What can I do to get the error messages into errors.txt?

© Server Fault or respective owner

Related posts about powershell

Related posts about search