Logging Script Output

by Sep 13, 2017

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
$a.Count

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

Twitter This Tip! ReTweet this Tip!