I am trying to write the entire output (errors included) of an executing script to the console and a file at the same time. I have tried several different options:
.\MyScript.ps1 | tee -filePath C:\results.txt # only the output to the file .\MyScript.ps1 2> C:\results.txt # only the errors to the file and not the console .\MyScript.ps1 > C:\results.txt # only the output to the file and not the console
My hope was that I could use the file to review the output/errors.
EDIT:
This is my current test script. The desired results is that all three messages can be seen.
function Test-Error { echo "echo" Write-Warning "warning" Write-Error "error" } Test-Error 2>&1 | tee -filePath c:\results.txt
To send a PowerShell command's output to the Out-File cmdlet, use the pipeline. Alternatively, you can store data in a variable and use the InputObject parameter to pass data to the Out-File cmdlet. Out-File saves data to a file but it does not produce any output objects to the pipeline.
There are two PowerShell operators you can use to redirect output: > and >> . The > operator is equivalent to Out-File while >> is equivalent to Out-File -Append . The redirection operators have other uses like redirecting error or verbose output streams.
Writes the specified objects to the pipeline. If Write-Output is the last command in the pipeline, the objects are displayed in the console.
The Out-File cmdlet is most useful when you want to save output as it would have displayed on the console. For finer control over output format, you need more advanced tools.
Have you tried:
.\MyScript.ps1 2>&1 | tee -filePath c:\results.txt
2>&1
is what you're looking for
Note: This answer works great in PowerShell 1.0 and 2.0, but will capture ONLY standard output and errors in PowerShell 3.0 and later.
I wasn't satisfied with any answer I was finding, so I mixed a few and came up with this (in PowerShell 3.0+):
$output = try{your_command *>&1}catch{$_}
With this you can capture all errors and output that are generated by trying to use your_command
.
It catches exceptions when you use a non-existent command:
PS C:\Users\jdgregson> $output = try{your_command *>&1}catch{$_} PS C:\Users\jdgregson> echo $output your_command : The term 'your_command' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:15 + $output = try{your_command 2>&1}catch{$_} + ~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (your_command:String) [], Comman dNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\jdgregson>
It catches exceptions when you pass invalid arguments to an existing command:
PS C:\Users\jdgregson> $output = try{cat C:\invalid-path.txt *>&1}catch{$_} PS C:\Users\jdgregson> echo $output cat : Cannot find path 'C:\invalid-path.txt' because it does not exist. At line:1 char:15 + $output = try{cat C:\invalid-path.txt 2>&1}catch{$_} + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\invalid-path.txt:String) [Ge t-Content], ItemNotFoundException + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetCo ntentCommand
And it catches the output if there was no problem with your command at all:
PS C:\Users\jdgregson> $output = try{cat C:\valid-path.txt *>&1}catch{$_} PS C:\Users\jdgregson> echo $output this file is really here
It works for your example too:
PS C:\Users\jdgregson> $output = try{Test-Error *>&1}catch{$_} PS C:\Users\jdgregson> echo $output echo WARNING: warning Test-Error : error At line:1 char:15 + $output = try{Test-Error *>&1}catch{$_} + ~~~~~~~~~~~~~~~ + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorExcep tion + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorExceptio n,Test-Error
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With