Powershell: how to capture PowerShell output

I have a set of powershell scripts that are invoked from the command line. Currently, the script returns a success or failure code.

Sometimes some powershell commands in scripts cause an error. I want to capture all the output (logs / message / error). I found that I can use a command like

. \ test.ps1 2> & 1 | foreach-object {$ _. ToString ()} | Out-File e: \ log.txt

Although this protocol logs an error message, the following problems

  • It simply logs the error message, not the line number that appears when I run the same script from the powershell console.

  • Now there is no output from the command line. I do not want to hide the output from the console and I want the entry in PowerShell to be optional.

  • I need to change the script invocation command. Is there any separate tool that can trigger and capture the output coming from the powershell window.

+3
source share
2 answers

I would like to mention Start-Transcript as a great way to do this.

You can also use Get-History to find out which commands you entered.

+2
source

Just found the Start-Transcript and Stop-Transcript commands. We can use it to capture all output content (error / message / info etc) into a file.

+2
source

All Articles