Alex Rivera | Logout

Redirect Write-Host statements to a file

Asked 2011-04-07T23:10:34.830
31

I have a PowerShell script that I am debugging and would like to redirect all Write-Host statements to a file. Is there an easy way to do that?

Edit
Report

2 Answers

9

Using redirection will cause Write-Host to hang. This is because Write-Host deals with various formatting issues that are specific to the current terminal being used. If you just want your script to have flexibility to output as normal (default to shell, with capability for >, 2>, etc.), use Write-Output.

Otherwise, if you really want to capture the peculiarities of the current terminal, Start-Transcript is a good place to start. Otherwise you'll have to hand-test or write some complicated test suites.

answered 2011-12-23T01:18:10.727
1

I just added Start-Transcript at the top of the script and Stop-Transcript at the bottom.

The output file was intended to be named <folder where script resides>-<datestamp>.rtf, but for some reason the trace file was being put where I did not expect it — the desktop!

answered 2011-04-08T18:02:35.850

Your Answer