Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ErrorHandlingDebugging-McElreath/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Take your PowerShell to the next level with error handling and debugging

There are a lot of great features for debugging PowerShell withing VSCode. And if you're like me.... you never use them....

Let's change that today. I've made it my goal this year to be better and more consistent with how I debug my code and handle errors in my scripts.

In this session, we're going to explore how errors are surfaced in PowerShell and how to handle those errors responsibly. We will also dive into using the debugger in VSCode to step through some PowerShell code to identify the potential sources of our errors.
91 changes: 91 additions & 0 deletions ErrorHandlingDebugging-McElreath/demo/1-Error-Variable.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@

$Error.Clear()

# Let's Generate some errors
1/0
Get-ChildItem c:\Does-Not-Exist
Get-WrongCommand

# Returns an array of error records, with the most recent error at index 0
$Error

# Most recent error
$Error[0]

# The oldest error
$Error[-1]

# Can be used in a catch block to get the current error
$_

try {
1/0
} catch {
Write-Host "An error occurred: $_"
}

# Was the last command successful?
Get-WrongCommand
$?

# Clear the error variable
$Error.Clear()
$Error

# Generate some more errors
Get-ChildItem c:\Does-Not-Exist

# Error view options
$ErrorView # ConciseView, CategoryView, DetailedView, NormalView

$ErrorView = 'DetailedView'

Get-ChildItem c:\Does-Not-Exist


$ErrorView = 'ConciseView'


# Get error details
$Error[0]

$Error[0] | Select-Object *

# Get the most recent error record with more information
Get-Error -Newest 1

# Get a text dump of the most recent error record
$Error[0] | Format-Custom -Force


$Error[0] | Select-Object *

$Error[0].Exception
$Error[0].InvocationInfo
$Error[0].ScriptStackTrace
$Error[0].Exception.StackTrace




$Error.Clear()
cls

# ScriptStackTrace example
function Trace-Test {
function Get-BadPath { Get-ChildItem c:\Does-Not-Exist }

function Function1 { Get-BadPath }

function Function2 { Function1 }

function Function3 { Function2 }

Function3

}

Trace-Test

$error[0] | Select-Object *

85 changes: 85 additions & 0 deletions ErrorHandlingDebugging-McElreath/demo/2-Terminating-Errors.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@

$Error.Clear()
cls


function Test-Error {
Get-ChildItem c:\Does-Not-Exist

Write-Host "Will this Print?" -ForegroundColor Magenta
}

Test-Error


function Test-Error {
Get-ChildItem c:\Does-Not-Exist -ErrorAction Stop

Write-Host "Will this Print?" -ForegroundColor Magenta
}

Test-Error


$ErrorActionPreference # Continue, SilentlyContinue, Ignore, Stop, Break, Inquire, Suspend


function Test-Error {
param (
[ValidateSet("Break", "Continue", "Ignore", "Stop", "SilentlyContinue", "Inquire" , "Suspend")]
$errorAction = "Continue"
)
$variable1 = "Hello"; $variable2 = "Summit 2026!"

Get-ChildItem c:\Does-Not-Exist -ErrorAction $errorAction

Write-Host "Will this Print?" -ForegroundColor Magenta
}

Test-Error

Test-Error -ErrorAction "Stop"
Test-Error -ErrorAction "SilentlyContinue"
Test-Error -ErrorAction "Ignore" # Does not add an error record to the $Error variable
Test-Error -ErrorAction "Suspend" # Only used in PowerShell Workflows
Test-Error -ErrorAction "Inquire"
Test-Error -ErrorAction "Break"



function Test-Error {
$ErrorActionPreference = "Stop"

Get-ChildItem c:\Does-Not-Exist

Write-Host "This will not be printed because ErrorActionPreference is set to Stop" -ForegroundColor Magenta
}

Test-Error


function Test-Error {
throw("An error occurred in Test-Error")

Write-Host "This will not be printed after a throw statement" -ForegroundColor Magenta
}

Test-Error



$Error.Clear()
$Error
cls

function Test-Error {
$ErrorActionPreference = "SilentlyContinue"

Get-ChildItem c:\Does-Not-Exist

Write-Host "This will be printed because ErrorActionPreference is set to SilentlyContinue" -ForegroundColor Magenta
}

Test-Error

$Error
Loading