Q. How can I suppress an error message in PowerShell?

To look at other examples on the Internet, you'd think putting this at the top of your script would be the answer, you don't want to do that. Click here for the solution!

Don Jones

August 18, 2010

1 Min Read
green binary code over a blurred mans face in background

Q. How can I suppress an error message in PowerShell?

A. To look at other examples on the Internet, you'd think putting this at the top of your script would be the answer:

$ErrorActionPreference = "SilentlyContinue"

Don't do that. Sure, it'll suppress errors in your script—ALL of the errors, even helpful ones about syntax errors and so on. Instead, if you anticipate a cmdlet causing an error such as "file not found" or "could not connect" and you don't want to see the error or deal with it, use the -ErrorAction (or -EA) parameter of that cmdlet:

Get-WmiObject Win32_BIOS -computername localhost,not-online -EA SilentlyContinue

All cmdlets have an -EA parameter. It's part of the listed in every cmdlet's help.

Do you have a Windows PowerShell question? Find more PowerShell FAQs, articles, and other resources at windowsitpro.com/go/DonJonesPowerShell.

 

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like