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.


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

Logging All Errors

In a previous tip you have seen that cmdlet errors can only be caught by an error handler if the -ErrorAction is set to "Stop". Doing this alters the way the cmdlet works, though. It will then stop at the first error encountered.

Take the next example: it scans the windows folder for PowerShell scripts recursively. If you wanted to catch errors (accessing protected subfolders, for example), this would not work:

try
{
  Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction Stop
}
catch
{
  Write-Warning "Error: $_"
} 

The code would catch the first error, but the cmdlet would stop and not continue to scan the remaining subfolders.

If you just suppressed errors, you would get complete results, but now your error handler would not catch anything:

try
{
  Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue
}
catch
{
  Write-Warning "Error: $_"
} 

So if you want a cmdlet to run uninterruptedly, and still get a list of all folders that you were unable to access, do not use an error handler at all. Instead, use -ErrorVariable and log the errors silently to a variable.

After the cmdlet has finished, you can evaluate the variable and get an error report:

Get-ChildItem -Path $env:windir -Filter *.ps1 -Recurse -ErrorAction SilentlyContinue -ErrorVariable myErrors

Foreach ($incidence in $myErrors)
{
    Write-Warning ("Unable to access " + $incidence.CategoryInfo.TargetName)
} 


Twitter This Tip! ReTweet this Tip!

Copyright 2012 PowerShell.com. All rights reserved.