Logging Script Output

There are numerous ways to log script output but one especially lazy technique is to run Start-Transcript. In PowerShell 5, this cmdlet is supported in all full hosts, not just powershell.exe, so you can use it in PowerShell ISE or other editors as well. In addition, transcripts can be nested, so when you write a script, you can safely add Start-Transcript at the beginning and Stop-Transcript at the end.

Start-Transcript writes any input and any output to a text file. If you do not specify a path, the cmdlet picks a default path. You just would have to make sure that your script actually produces output that can be logged.

To make your script be more verbose, combine it with another trick: when you place assignments in parentheses, the assignment will also output the assigned data. This output would then be picked up by your transcript. Check it out:

# default assignment, no output
$a = Get-Service

# assignment in parentheses, outputs the assignment
($b = Get-Service)

Twitter This Tip! ReTweet this Tip!

  • Parenthetical assignment is a trick I have use for a few years now and shown to many. Specifically, as another way of avoid the use of Write-* to output results to the console (ISE output pane/console or console host)

    The one thing to be cognizant of with parenthetical assignment use is you are using nesting.

    Parenthetical nesting will only output the outer assignment results.

    For example:

    Combining to a single variable.

    No outer parens

    $EmailAddress = ($Alias = (($DisplayName = 'Bill Gates') -replace ' ','.')) + "@$env:USERDNSDOMAIN"

    No results displayed

    Output to console - no Write-* needed, or quotes of any kind




    3 variables, one output result - again no Write-* needed, or quotes of any kind

    ($EmailAddress = ($Alias = (($DisplayName = 'Bill Gates') -replace ' ','.')) + "@$env:USERDNSDOMAIN")



    You only need Write-Host if you are using explicit formatting, for console output.

    Think, coloring output lines, etc...

    You don't even need Write-Host for combined variable output.

    For Example:

    ($OfficeSite = 'I have lots of money, so, where ever I choose to be 8^}')

    $EmailAddress + ' --- ' + $OfficeSite



    I have lots of money, so where ever I choose to be 8^}

    Bill.Gates@contoso.com --- I have lots of money, so, where ever I choose to be 8^}


       Write-Host [[-Object] <Object>]

       [-BackgroundColor <ConsoleColor>]

       [-ForegroundColor <ConsoleColor>]

       [-NoNewline ]

       [-Separator <Object>]


    Well, if you are not on the latest PoSH version you have that whole, it does not work on the pipeline, well, not without being really creative. 8^}

    (hence the whole killing puppies thingy.)

    For those who did not already know this -- See these article for details


    With PowerShell v5 Write-Host no longer "kills puppies". data is captured into info stream


    Of course you can, manually marquee select any of the variables, or enclosed parenthetical blocks to print out its content, but that's not a thing with Start-Transcript