Q. How can I capture and examine any errors produced by a cmdlet?Q. How can I capture and examine any errors produced by a cmdlet?
Much like the -EA parameter, all cmdlets have a common parameter called -ErrorVariable (or -EV). Specify the name of a variable (which does not include the $ in front of the name), and any errors will be put into that variable.
Don Jones
August 23, 2010
1 Min Read
Q. How can I capture and examine any errors produced by a cmdlet?
A. Much like the -EA parameter, all cmdlets have a common parameter called -ErrorVariable (or -EV). Specify the name of a variable (which does not include the $ in front of the name), and any errors will be put into that variable. Here's an example:
Try { Gwmi Win32_BIOS -comp localhost,not-online -EA Stop -EV Err} catch { Write-Host "Uh, oh $Err happened" -fore red -back white}
To read about other common parameters, run
help about_common_parameters
in the shell.
Do you have a Windows PowerShell question? Find more PowerShell FAQs, articles, and other resources at windowsitpro.com/go/DonJonesPowerShell.
About the Author
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