PowerShell:
Printing debug output

How to:

Let’s keep it simple and actually do something. We’ll show the value of a variable, how a loop is progressing, and capture the pesky error that might pop up.

# Showing the value of a variable
$name = "PowerShell Guru"
Write-Host "The value of name is: $name"

# Monitoring a loop's progress
for ($i = 0; $i -lt 5; $i++) {
    Write-Host "We're on loop number: $i"
}

# Capturing and printing an error
try {
    Get-Item "C:\NonExistent\File.txt" -ErrorAction Stop
} catch {
    Write-Host "Oops: $_"
}

Sample output:

The value of name is: PowerShell Guru
We're on loop number: 0
We're on loop number: 1
We're on loop number: 2
We're on loop number: 3
We're on loop number: 4
Oops: Cannot find path 'C:\NonExistent\File.txt' because it does not exist.

Deep Dive

Back in the ancient days of computing, debugging often meant literal physical bugs messing with the hardware. We’ve come a long way since then, now using the term “bug” for code problems, and “debugging” for fixing them.

The Write-Host cmdlet is the PowerShell buddy for printing to the screen, which is fine for basic scripts. But there are cooler ways to do it: Write-Verbose, Write-Debug, Write-Output, and Write-Information are like different flavors of output for various use cases. They give you controlled verbosity, which is awesome when you need to silence your script or log stuff without spamming the console.

When it comes to implementation, PowerShell’s error handling is particularly swanky. You can catch different types of exceptions with try, catch, finally blocks and decide how to react. It’s like a choose-your-own-adventure for error management.

See Also