Redirect write-host outputs to text file


I am trying to export the write-host output to text file, but it’s not happening, can someone please help

write-host “hello world” | out-file .\log.txt

In a script, I want to save all the results of write-host commands

.\test.ps1 3>&1 2>&1 4>&1 6>&1|Out-File .\log.txt

The above command is able to save only warnings, errors and verbose but not the result of write-host. Is there any way to save them ?

Write-Host only displays output onscreen. It does not send anything down the pipeline, so you can’t redirect its output since there is nothing to redirect.

1 Like

Look into




This will write any consoleoutput to a file…

Or use write-output instead of write-host

write-host hello world 6>&1 | out-file .\log.txt

Or just

write-output 'hello world' | out-file .\log.txt

Let me ask you something? Do you actually sit in front of your computer and watch your scripts doing their work? If the answer is no you don’t need Write-Host regularly in your scripts anymore. :wink:

Instead you can use Write-Debug or Write-Verbose if you like to know what your scripts are doing during the process of developing. Or you simply use something like this "Log message" | Out-File -FilePath $Path if you like to log something to a file. :wink:

If you use Start-TranScript, Write-Host will go to the file you define as well as sdtOut (the display/Shell)

@Olaf, I used “Log message” instead of write-host.

and did this way:

Test_log 2>&1 4>&1 6>&1|Out-File C:\temp\log.txt

I was able to get the log messages in the log file, however, now I am not able to view it on console :expressionless:

with just calling function, I am able to see “Log message” and also the verbose messages which are contained in the function, but when I redirect them, won’t they be shown again on the console?

Could you suggest me a way ?

Stop watching your scripts on the console! :wink: :smiley: … I think the issue is not technical it is logical. Either you want to have scripts usually running unattended in the background wíthout user intaction or you run them manually in the console. For the first one you do not need any console output. For the latter one you do.
If you want to debug scripts where you implemented extended logging you should watch the log files if you want to know what’s going on.

If you don’t want to re-invent the wheel - there are frameworks out there offering extended logging and very flexible functionalities. You may use one of them.

Thanks Olaf, This cleared my query