PowerShell:
Handling errors

How to:

# Basic Try-Catch to handle exceptions
try {
    # Code that might trigger an error
    $result = 1 / 0
} catch {
    # What to do if an error occurred
    Write-Host "Oops, an error occurred: $_"
}

# Outputting a custom error message
try {
    Get-Item "nonexistentfile.txt" -ErrorAction Stop
} catch {
    Write-Host "The file couldn't be found."
}

# Using the $Error variable to inspect the last error

Deep Dive

PowerShell has come a long way since its inception as Monad. Error handling became more robust over time, offering features similar to other programming languages. The try-catch-finally syntax is one such cross-pollination from languages like C#. Before it, scripters relied heavily on checking conditions and using the $Error automatic variable.

PowerShell also has two main types of errors: terminating and non-terminating. Terminating errors will halt the script unless caught in a try-catch block, while non-terminating ones won’t unless you specify -ErrorAction Stop. This distinction is crucial as it grants fine control over error handling, deciding whether an error truly warrants stopping the entire script or can simply be logged and ignored.

PowerShell’s error handling allows for a finally block, too, which runs no matter what - whether an error occurred or not. It’s great for cleanup tasks.

When you’re deep in the scripting trenches, you can also handle specific exception types, giving you even finer control.

Alternatively, there’s the old school -ErrorVariable parameter to capture errors without throwing an exception. And the $? variable tells you if the last operation was successful. They’re handy tools, albeit a little less clean than a solid try-catch.

See Also