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:

  1. Try {
  2.   Gwmi Win32_BIOS -comp localhost,not-online -EA Stop -EV Err
  3. } catch {
  4.   Write-Host "Uh, oh $Err happened" -fore red -back white
  5. }

To read about other common parameters, run

  1. 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.