Resovling PowerShell errors

This tip was contributed to WServerNews by Ashley McGlone, Microsoft Premier Field Engineer.

PowerShell Errors Tip

Do you ever get that red text at the PowerShell console?  Yes?  So do I.  The only person that doesn’t is Chuck Norris.  The PowerShell error text is usually quite descriptive, but sometimes you need to know more about what happened.

When you are working in PowerShell all errors by default go into the $Error array.  This array stores up to the last 256 errors that occurred, and the most recent error is always inserted at index zero.  You can view the most recent error by typing:

$Error[0]

To get all of the juicy details, you can pipe it to Format-List like this:

$Error[0] | fl * -Force

You have to use the Force switch, because normally the extra details are hidden.

To find out more about where the error originated use this:

$Error[0].InvocationInfo

To find out what happened leading up to the error view the stack trace:

$Error[0].Exception.StackTrace

These few extra moves should get you closer to the root cause of your error.  Happy hunting!

About Ashley McGlone

Ashley McGlone is a Microsoft Premier Field Engineer (PFE) at Microsoft.

You can find more of Ashley’s PowerShell tips on his TechNet blog here:

http://aka.ms/GoateePFE

The above tip was previously published in an issue of WServerNews, a weekly newsletter from TechGenix that focuses on the administration, management and security of the Windows Server platform in particular and cloud solutions in general. Subscribe to WServerNews today by going to http://www.wservernews.com/subscribe.htm and join almost 100,000 other IT professionals around the world who read our newsletter!

Mitch Tulloch is a twelve-time recipient of the Microsoft Most Valuable Professional (MVP) award and a widely recognized expert on Windows Server and cloud computing technologies.  Mitch is also Senior Editor of WServerNews. For more information about him see http://www.mtit.com.

 

Leave a Comment

Your email address will not be published.

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Scroll to Top