Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell: send console output to file without deafening this console output

I have a lot of PowerShell script. One main, that calls other, child ones. Those PS scripts in their turn call windows CMD scripts, bash scripts and console applications. All these scripts and applications write messages to console. PowerShell scripts, for example, are using Write-Host scriptlet for this purpose.

Question: how can I easely redirect (send) all this console output to some file, while not deafening (canceling) this console output? I want to be able to see whats going on from console output and also have history of messages in log file.

Thanks.

like image 982
Roman Avatar asked Feb 24 '11 21:02

Roman


People also ask

What cmdlets can redirect output into a file or to a printer?

The Out-Printer cmdlet sends output to the default printer or to an alternate printer, if one is specified. This cmdlet was reintroduced in PowerShell 7. This cmdlet is only available on Windows systems that support the Windows Desktop.

How do I write a PowerShell script to a file?

You can also use PowerShell to write to file by appending to an existing text file with Add-Content Cmdlet. To append “This will be appended beneath the second line” to our existing file, first-file. txt, enter this command and press enter.


2 Answers

You can use the tee equivalent of PowerShell : Tee-Object

PS: serverfault.com and/or superuser.com are more suitable for a question like this.

like image 55
Diadistis Avatar answered Oct 06 '22 04:10

Diadistis


You can try Start-Transcript and Stop-Transcript. It has a couple of limitations like not capturing native exe output. It is also global to PowerShell session.

like image 37
Keith Hill Avatar answered Oct 06 '22 04:10

Keith Hill