Tired of juggling PowerShell error handling? Discover how to simplify your workflow with a versatile function that converts error records into easy-to-manage data objects, making troubleshooting smoother than ever:

Translating errors From error records to data

In our mini-series, we got in touch with exceptions in various ways: through $_ in a catch block or a trap, via $error or separate error variables defined using -ErrorVariable, or by redirecting the error stream to the pipeline.

In all of these cases, you receive error objects. Here is a generic function you can use to easily convert all of these error records to convenient data objects:

filter Convert-ErrorRecord 

{

    [PSCustomObject]@{

        Exception = $_.Exception.Message

        Reason    = $_.CategoryInfo.Reason

        Target    = $_.CategoryInfo.TargetName

        Script    = $_.InvocationInfo.ScriptName

        Line      = $_.InvocationInfo.ScriptLineNumber

        Column    = $_.InvocationInfo.OffsetInLine

    }

}

This function works in all the error handlers we have discussed in this series.

To recapitulate, here are all four error handling strategies we discussed in this series, all using Convert-ErrorRecord to turn the exceptions into simple-to-handle data objects. Just make sure you define above Convert-ErrorRecord function by running it, before trying one of below examples:

With try/catch:

try 

{

    Write-Host 'Previous Command'

    $result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log -ErrorAction Stop

    Write-Host 'Next Command'

}

catch 

{

    $_ | Convert-ErrorRecord

}

With trap:

Write-Host 'Previous Command'

$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log -ErrorAction Stop

Write-Host 'Next Command'



trap {

    $_ | Convert-ErrorRecord

    continue

}

With $Error:

$error.Clear()



# hide error messages to the user

$ErrorActionPreference = 'SilentlyContinue'



# perform cmdlet actions, i.e., find log files

$result = Get-ChildItem -Path $env:windir -Depth 1 -Filter *.log 



# perform more actions, i.e., access a service that does not exist

$service = Get-Service -Name notThere 



# restore default error action

$ErrorActionPreference = 'Continue'



# check errors after the fact

$errorCount = $error.Count

Write-Host "There were $errorCount errors."



# output error details

$error | Convert-ErrorRecord

With Error Stream:

 Get-ChildItem -Path c:\windows -Depth 1 -Filter *.log 2>&1 | ForEach-Object {

  if ($_ -is [System.Management.Automation.ErrorRecord])

  {

      $text = $_ | Convert-ErrorRecord | Out-String 

      Write-Host $text 

  }

  else

  {

      $_

  }

}  
 

So these were all four blog posts in October:

Your ultimate PowerShell Cheat Sheet

Unleash the full potential of PowerShell with our handy poster. Whether you're a beginner or a seasoned pro, this cheat sheet is designed to be your go-to resource for the most important and commonly used cmdlets.The poster is available for download and in paper form.

Get your poster here!

Related links