Atul Sureka
Atul Sureka

Reputation: 3302

Powershell : How to capture powershell output

I have set of powershell scripts which gets invoke from command prompt. Currently script returns the success or failure code.

Sometime some of the powershell commands in the scripts throws error. I want to capture all the output (logs/message/error). I found that I can use the command like

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

Although this approch logs the error message however following are the problems

  1. It just logs the error message not the line number which comes when I run the same script from the powershell console.

  2. Now there is no output message from powershell. I do not want to hide the output from console & want powershell logging as additional thing.

  3. I have to modify the script invocation command. Is there any standlone tool which can run & capture the output coming on the powershell window.

Upvotes: 3

Views: 1966

Answers (2)

Thomas Lee
Thomas Lee

Reputation: 1168

I was going to mention Start-Transcript as a great way to do it.

You can also make use of Get-History to find out what commands you entered.

Upvotes: 2

Atul Sureka
Atul Sureka

Reputation: 3302

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

Upvotes: 2

Related Questions