Error Handling

Try / Catch / Finally

#Try block
try {
    $ErrorCount = 0
    #Creating an error
    Get-NotARealCommand
}
catch {
    $ErrorCount = $ErrorCount + 1
    #Writing the error to the console including the line number it was on
    Write-Host "Error Message: $($_.Exception.Message) Error on line $($_.InvocationInfo.ScriptLineNumber)"
    Write-Host "Trying command #2"
    Get-TimeZone | Format-Table id
}
finally {
    #At the end of the catch do one last thing "finally"
    Write-Host "Error count is $ErrorCount"
}
Error Message: The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again. Error on line 3
Trying command #2

Id
--
Pacific Standard Time

Error count is 1

Error Stream

$Error[0]
Get-NotARealCommand:
Line |
   3 |      Get-NotARealCommand
     |      ~~~~~~~~~~~~~~~~~~~
     | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program.
Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
$Error[0] | fl * -Force
PSMessageDetails      :
Exception             : System.Management.Automation.CommandNotFoundException: The term 'Get-NotARealCommand' is not
                        recognized as the name of a cmdlet, function, script file, or operable program.
                        Check the spelling of the name, or if a path was included, verify that the path is correct and
                        try again.
                           at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext
                        funcContext, Exception exception)
                           at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame
                        frame)
                           at
                        System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                        frame)
                           at
                        System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame
                        frame)
TargetObject          : Get-NotARealCommand
CategoryInfo          : ObjectNotFound: (Get-NotARealCommand:String) [], CommandNotFoundException
FullyQualifiedErrorId : CommandNotFoundException
ErrorDetails          :
InvocationInfo        : System.Management.Automation.InvocationInfo
ScriptStackTrace      : at <ScriptBlock>, <No file>: line 3
PipelineIterationInfo : {}

Error Action

$ErrorActionPreference = 'Stop'
Get-NotARealCommand
Write-Host "This text is after the error"
Line |
   2 |  Get-NotARealCommand
     |  ~~~~~~~~~~~~~~~~~~~
     | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
     | correct and try again.
$ErrorActionPreference = 'Continue'
Get-NotARealCommand
Write-Host "This text is after the error"
Line |
   2 |  Get-NotARealCommand
     |  ~~~~~~~~~~~~~~~~~~~
     | The term 'Get-NotARealCommand' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is
     | correct and try again.

This text is after the error
$ErrorActionPreference = 'SilentlyContinue'
Get-NotARealCommand
Write-Host "This text is after the error"
This text is after the error