PowerShell Scripts, Tips, Expert Advices, Forums, and Resources

image rotation

Welcome to PowerShell.com, the educational and community site for Windows PowerShell People. Get a quick overview.

Latest Forum Posts

04-16-2014 - How Do I [Split Replace RegEx]
04-17-2014 - How do I trim a line in a .txt file and append the...
04-13-2014 - XML Code

As a Powershell.com member you will have access to:

  • Daily PowerShell tips written by Microsoft MVPs and other leading Windows PowerShell experts
  • Free Windows PowerShell advice and training provided by Microsoft MVPs and other leading Windows PowerShell experts
  • Access to leading Windows PowerShell blogs
  • A free ebook, Mastering PowerShell, written by Microsoft MVP Dr. Tobias Weltner
PowerTip of the Day

Catching Non-Terminating Errors

Non-terminating errors are errors that are handled by a cmdlet internally. Most errors that can occur in cmdlets are non-terminating.

You cannot catch these errors in an error handler. So although there is an error handler in this sample, it will not catch the cmdlet error:

try
{
  Get-WmiObject -Class Win32_BIOS -ComputerName offlineIamafraid 
}
catch
{
  Write-Warning "Oops, error: $_"
} 

To catch non-terminating errors, you must turn them into terminating errors. That is done by setting the -ErrorAction to "Stop":

try
{
  Get-WmiObject -Class Win32_BIOS -ComputerName offlineIamafraid -ErrorAction Stop
}
catch
{
  Write-Warning "Oops, error: $_"
} 

You can temporarily set $ErrorActionPreference to "Stop" if you do not want to add a -ErrorAction Stop parameter to all cmdlets within your error handler. The preference is used if a cmdlet does not explicitly specify an -ErrorAction setting.

Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.