Skip to content

Common PowerShell Exceptions

PowerShell Logo

Each specific exception in PowerShell is designed to handle a particular type of error or exceptional condition that may occur during script execution. It’s not feasible to provide an exhaustive list of all possible exceptions in PowerShell, as the language is highly extensible and allows for custom exceptions to be defined by users or modules.

Each exception type in PowerShell is typically defined within the System namespace, and some exceptions may have additional namespaces depending on their origin or context. When catching exceptions in PowerShell, you can either specify the fully qualified name of the exception class (including its namespace) or use the short name if it’s available in the current namespace.

For example, the ItemNotFoundException exception belongs to the System.Management.Automation namespace. So, you can catch it using either its fully qualified name [System.Management.Automation.ItemNotFoundException] or just ItemNotFoundException if you’re already in the System.Management.Automation namespace context.


PowerShell Exceptions

  • ItemNotFoundException: This exception is thrown when an attempt is made to access an item that does not exist, such as a file or directory.
  • CommandNotFoundException: Occurs when PowerShell cannot find a specified command. This can happen when trying to execute a command that doesn’t exist or isn’t available in the current environment.
  • ParameterBindingException: Thrown when there is an issue binding a parameter to a cmdlet or function. This can occur when the provided parameter values do not match the expected types or when required parameters are missing.
  • InvalidDataException: Indicates that the data supplied to a cmdlet or function is invalid. This can happen when providing input that does not meet the expected format or constraints.
  • InvalidOperationException: Typically occurs when an operation is performed that is not valid for the current state of the object. This can include trying to perform operations on closed or disposed objects.
  • UnauthorizedAccessException: Thrown when the user does not have permission to perform a particular operation. This can occur when trying to access or modify files or settings without the necessary permissions.
  • RuntimeException: This is a generic exception class that serves as the base class for all exceptions thrown by PowerShell. It typically indicates an unexpected error or problem during script execution.
  • ArgumentException: Indicates that one or more arguments provided to a cmdlet or function are invalid. This can include providing arguments with incorrect types or values.
  • ArgumentNullException: Thrown when a null argument is passed to a method that does not accept it. This can occur when passing null values to cmdlets or functions that expect non-null arguments.
  • FormatException: Occurs when the format of an argument does not meet the requirements of the cmdlet or function. This can include providing strings that cannot be parsed into the expected format.
  • PipelineStoppedException: Indicates that the pipeline has been stopped, typically due to an error or user intervention. This can occur when an error is encountered during pipeline execution.
  • ScriptCallDepthException: This exception occurs when the maximum script call depth has been exceeded. This can happen when scripts or functions recursively call themselves too many times.
  • NotSupportedException: Indicates that a particular operation is not supported by PowerShell. This can occur when trying to perform operations that are not implemented or allowed in the current environment.
  • TimeoutException: Thrown when an operation exceeds the specified timeout period. This can occur when executing long-running operations that take longer than expected to complete.
  • IOException: Thrown for I/O related errors, such as file or network access issues. This can occur when there are problems reading from or writing to files, directories, or network resources.
  • OutOfMemoryException: Indicates that the system has run out of memory to complete the operation. This can occur when scripts or processes consume more memory than is available.
  • PipelineClosedException: Indicates that the pipeline has been closed unexpectedly. This can occur when attempting to write to a closed pipeline or when a pipeline is closed due to an error.
  • ProviderNotFoundException: Thrown when a specified provider cannot be found. This can occur when trying to access a PowerShell provider that is not available in the current environment.
  • SessionStateUnauthorizedAccessException: Indicates that access to a session state variable is unauthorized. This can occur when trying to access or modify session state variables without the necessary permissions.
  • WildcardPatternException: Occurs when a wildcard pattern used in a command parameter is invalid. This can happen when providing wildcard patterns that contain syntax errors or are not properly formatted.

Example Usage:

try {
    # Attempt to access a file that doesn't exist
    $file = Get-Item "C:\Path\To\Nonexistent\File.txt"
 
    # If the file exists, display its name
    Write-Output "File found: $($file.FullName)"
}
catch [System.Management.Automation.ItemNotFoundException] {
    # Catch the ItemNotFoundException and handle it
    Write-Output "File not found: $($Error[0].Exception.Message)"
}
catch {
    # Catch any other exceptions
    Write-Output "An error occurred: $($_.Exception.Message)"
}

Published inPowerShell
© 2024 ScriptWizards.net - Powered by Coffee & Magic