How to determine the exception/error type for use in Catch block

When an error occurs running a command, how can you determine the error type so that you can catch it in a Try/Catch statement? As a specific example, if you run the commands to connect to Exchange Online twice in the same console, you’ll get a warning saying that the proxy creation has been skipped for some commands, followed by the below error:

Import-PSSession : No command proxies have been created, because all of the requested remote commands would shadow existing local commands. Use the AllowClobber parameter if you
want to shadow existing local commands.
At C:\Users\miracles1315\OneDrive\PowerShell\Scripts\Office 365\Connect-O365ExchangeOnline\Connect-O365ExchangeOnline.ps1:129 char:39

  • … bal:EXOExportedCommands = Import-PSSession $Session -ErrorAction Stop
  •                           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : InvalidResult: (:slight_smile: [Import-PSSession], ArgumentException
    • FullyQualifiedErrorId : ErrorNoCommandsImportedBecauseOfSkipping,Microsoft.PowerShell.Commands.ImportPSSessionCommand

I’m trying to specifically catch for the above error, but I’m also curious how to determine the error type to catch for, including how to determine what parent that inherited type comes from.

I found this article helpful for determining error/exception types: Quick Hits: Finding Exception Types with PowerShell | Learn Powershell | Achieve More

Thanks Matt. I never found that article. :slight_smile: