PowerShell:
Logging

How to:

Here’s the lowdown on sprinkling some basic logging into your scripts:

# Creating a simple log message
Write-Host "Info: Starting the script process."

# Writing to a file
"Info: This is a logged message." | Out-File -Append myLog.log

# Using the built-in cmdlet for more detailed logging
Start-Transcript -Path "./detailedLog.log"
Write-Output "Warning: Something isn't quite right."
# ... your script does stuff
Stop-Transcript

# Output of detailedLog.log
******************************
Windows PowerShell transcript start
Start time: 20230324112347
Username  : PShellGuru@example.com
RunAs User: PShellGuru@example.com
Configuration Name: 
Machine  : PS-DEVBOX (Microsoft Windows NT 10.0.17763.0)
Host Application: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Process ID: 2024
PS Version: 7.1.2

Now, in your logs, there’s a play-by-play of what your code’s been up to.

Deep Dive:

Historically, logging’s about as old as programming itself. It’s like a captain’s log but for software. Back in the day, it might’ve been printouts or teletype machines; now it’s all about files and fancy logs management systems.

When you’re down in the PowerShell trenches, Write-Host is quick and dirty, but it just spits out text to the console, not great for keeping records. Out-File gives you a simple way to throw text into a file, but for the real juice, you’ll want Start-Transcript and Stop-Transcript which log everything—input, output, the works.

Alternatives? Sure, if you’re rolling enterprise, you might look at Windows Event Log or using software like Logstash, but for your day-to-day script, stick with PowerShell’s tools. As for implementation, remember to log smart – too little and it’s useless, too much and it’s white noise.

See Also:

Check out these to get a handle on all things logging in PowerShell: