Both version 1 and version 2 of Windows PowerShell have a nasty limitation when it comes to capturing *all* output from a script. First up, within a script there is no way to redirect host (Write-Host), verbose, warning and debug message to a log file. There is a mechanism within PowerShell that allows you to capture these streams of information – Start-Transcript. At first blush, this seems promising however the wheels fall off pretty quickly primarily because Start-Transcript doesn’t capture the output of EXEs. So all that output from compilers, build tools, etc doesn’t get captured. That’s a huge, gaping hole. Furthermore, Start-Transcript is really aimed at capturing your entire PowerShell session. It isn’t particularly suited for script logging. For instance, some folks including myself use Start-Transcript in their profile to capture an entire PowerShell session. This is very handy when you need to look up how you did something previously which is possible because you can search your transcript files. However, if you start a script that blindly calls Start-Transcript it will error. You can only call Start-Transcript if it hasn’t already been started . There is no notion of nested transcripts.
If you find these limitations as annoying as I do, please vote on them via the Microsoft Connect site:
For now, the best way to accomplish this is to use a host that can capture all PowerShell output. It turns out that executing the script through another instance of PowerShell.exe will allow you to capture all the output. For instance, consider the following script (test.ps1) that exercises the various output streams:
If you run the following you can easily capture all PowerShell output as shown below:
PS> powershell.exe -noprofile -file test.ps1 > test.log PS> Get-Content test.log HILLR1 host output C:\Users\hillr\Bin\test.ps1 : error + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1 host VERBOSE: verbose WARNING: warning DEBUG: debug
Updated 3/7/09: Aleksandar pointed out that you can just use Powershell.exe. No need to go through cmd.exe. Thanks!
psmdtag:script – Logging output