Subscribe to Windows IT Pro
January 26, 2010 12:00 AM

Q. How can I avoid receiving errors from PowerShell commands when data is missing?

Windows IT Pro
InstantDoc ID #103486
Rating: (0)

A. PowerShell has an error action option that you can add to commands. The switch

-ea 0

will cause PowerShell to take no action when it encounters an error. This can be useful if you're checking for data that may not exist and would normally return an error.

For example, the command

get-hotfix -id KB974332 -computername savdaldc11

will return an error if the specified computer doesn't have hotfix KB974332 installed. If you instead use the command

get-hotfix -id KB974332 -computername savdaldc11 -ea 0

nothing will happen and you won't get an error message.

The -ea 0 switch is actually equivalent to the command -ErrorAction SilentlyContinue, as in

get-hotfix -id KB974332 -computername savdaldc11 -ErrorAction SilentlyContinue

Other options for the error action are stop (1), continue (2) and inquire (3). The default action is continue, which you can check by viewing the $ErrorActionPreference value with the command

$ErrorActionPreference

The stop option halts execution in the event of an error and inquire asks the user whether to continue or halt.

Related Reading:



Check out hundreds more useful Q&As like this in John Savill's FAQ for Windows. Also, watch instructional videos made by John at ITTV.net.

Related Content:

ARTICLE TOOLS

Comments
    There are no comments to display. Be the first one!
You must log on before posting a comment.

Are you a new visitor? Register Here

advertisement

advertisement

Windows is a trademark of the Microsoft group of companies. Windows IT Pro is used by Penton Media Inc. under license from owner.