Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 296
Atul Sureka Avatar asked Jul 12 '26 00:07

Atul Sureka


1 Answers

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.

like image 129
Thomas Lee Avatar answered Jul 13 '26 21:07

Thomas Lee



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!