In PowerShell how to capture error, warning, write-host output into a single file?

10,005

Solution 1

The simple way to do this is to use cmd.exe e.g.:

cmd /c powershell.exe -file <path_to_script> > script.log

Solution 2

Have a look at PowerShell 2.0: A Configurable and Flexible Script Logger Module by Oisin. It should be possible to prefix the message with your custom string ('ERROR', 'WARN',...)

Solution 3

Perhaps Start-Transcript and Stop-Transcript will do what you want. (PowerShell 2.0)

Share:
10,005
Harsha
Author by

Harsha

Updated on June 16, 2022

Comments

  • Harsha
    Harsha almost 2 years

    In PowerShell how to capture error, warning, write-host output into a single file?

    When I execute the external command/ write-warning/ write-error/ write-host I need all the information to be captured in a file.

    when I redirect the write-error to a log file it will not show the same content as it's been displayed on the console. It will have some other information which I don't need.

    Is it possible to prefix error with ERROR: , warning with WARN: in the log file?

  • Keith Hill
    Keith Hill about 14 years
    Start-Transcript will capture those other streams but it doesn't capture any output from native commands (console exes). That's a fatal flaw for a lot of folks.