PowerShell Logging — Recording the Output of Your Scripts

PowerShell is one of the most powerful tools in an administrator's toolbox, and I use it daily. The more I do with PowerShell, the more I need to record my script and save my script output as reference or proof of the changes made. For example, when did that condition-based automation fire, and what did it do, or what data did you update from that data extract from HR against AD? PowerShell provides several options, and here are my favorites.

Write-Host

The simplest option is the "Write-Host" command, which returns the output to the command window. This is great for returning simple values; you can watch as the script runs. By including the variable after the write host command, you can print its value at that point in the code for reference-


$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$message = "Logging Blog"

Write-Host "$timestamp - $message"

Typically, this wouldn't record the output anywhere other than in the window like above. However, if you're working with an RMM platform like NinjaOne, Write-Host adds the results to the device log, making it great to see them later. If you cannot use a similar tool or need the outputs of commands, you can pair Write-Host with the Start-Transcript command.

Start-Transcript

The Start-Transcript is the place to go when you need to record the output of commands as a script runs, without adding any additional commands. To use the command, tell it where to save the log file, and you're off. Optional switches I usually include are the "-Append" flag, which adds the contents to a file at the location if it exists. Without this flag, a new file will be created every time. In later versions of PowerShell (6.2+), you can add the "-UseMinimalHeader" to shorten the additional information.


$logpath = "c:\temp\LoggingBlog.log"
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$message = "Logging Blog"

Start-Transcript -Path $logpath -Append -UseMinimalHeader
Write-Host "$timestamp - $message"
Stop-Transcript

The transcript file contains anything that happened inside the transcription window, making it helpful for debugging if you're getting script issues. As shown in the above code, be sure to include the "Stop-Transcript" command to end the file and ensure you don't capture more than you need.

Add-Content

For instances where I want to add information to files as the script runs, my go-to is "Add-Content". By default, this command appends data to a path supplied in the script with the "-path" and "-value" flags. It provides a clean way to record changes in a script containing hundreds of steps, iterates many times through a loop, or is part of an ongoing task.


$logpath = "c:\temp\LoggingBlog2.log"
$timestamp = (Get-Date).ToString("yyyy-MM-dd HH:mm:ss")
$message = "Logging Blog"

Add-Content -Path $logPath -Value "$(Get-Date): Some Text"

The one downside to this command is the potential to create an ever-growing log file. However, this can be easily fixed by implementing a log rotation strategy I'll cover in my next post.


Comments