Write stderror to a file using PowerShell
- by Zian Choy
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?