Posey's Tips & Tricks
How PowerShell 7 Makes Better Sense of Error Messages
Earlier versions of PowerShell only had one way to display errors, and it wasn't always the most helpful. Microsoft's support of the Get-Error cmdlet in PowerShell 7 makes error messages much more useful.
If you have worked with PowerShell at all, you have no doubt encountered your share of errors (don't worry, I get a lot of errors, too). The thing about PowerShell errors is that that they aren't always displayed in the most helpful way.
I've seen a relatively simple mistake fill my console screen with red, making it appear as though I have done something completely catastrophic. I have also encountered errors in automated scripts that disappear from the screen too quickly for me to read them. Thankfully, Microsoft has spent some time making PowerShell error messages much more palatable.
One of the more welcome additions to PowerShell 7 is a new cmdlet named Get-Error. Admittedly, this cmdlet's name sounds a little weird; nobody wants to actually get an error. However, the command is useful. Entering the Get-Error cmdlet causes PowerShell to show you the most recently produced error message.
If you look at Figure 1, you can see that the Get-Error cmdlet actually does quite a bit more than just tell you what the last error message was. It provides you with a wealth of diagnostic data.
What if you want to see more than just your most recent error? Well, PowerShell gives you a way to do that, too. All you have to do is to append the -Newest switch, followed by the number of errors that you want to examine. For instance, if you want to see your five most recent errors, all you have to do is enter the command shown below:
Get-Error -Newest 5
PowerShell 7 also gives you the ability to change the way that error messages are displayed. Before PowerShell 7, there was only one way to display error messages. If you look at Figure 2, for example, you can see the error that I get when I try to enter the Get-Error command into PowerShell version 5. This is the way that PowerShell has always displayed errors in the past.
In contrast, PowerShell 7 has three different ways to display error messages. You have already seen the first way in which errors can be displayed: Using the Get-Error cmdlet causes PowerShell to display the error in granular detail.
PowerShell can also display errors using what is known as concise view. Concise view is similar to the way that earlier PowerShell versions displayed error messages, but the error message is displayed without any supporting information. The end result is an error message that is a bit more direct and to-the-point. Incidentally, PowerShell 7 defaults to using concise view. You can see an example of a concise error message in Figure 3, where I have intentionally misspelled the Get-Error cmdlet.
The third option for displaying PowerShell errors is to display them in the normal way, similar to what you saw in Figure 2 earlier. PowerShell uses an environment variable called $ErrorView that controls the way that errors are displayed. If you want PowerShell to display errors similarly to the way that it did in the past, simply enter this command:
$ErrorView = 'NormalView'
If you look at Figure 4, you can see the difference between a concise error message and a "normal" error message.
If you want to set PowerShell back to using concise errors, just use this command:
$ErrorView = 'ConciseView'
You can see how this works in Figure 5.
As you can see, Microsoft has done a lot of work around the way that PowerShell errors are displayed. If you are feeling adventurous, you can even change the color of the error messages.
Brien Posey is a 16-time Microsoft MVP with decades of IT experience. As a freelance writer, Posey has written thousands of articles and contributed to several dozen books on a wide variety of IT topics. Prior to going freelance, Posey was a CIO for a national chain of hospitals and health care facilities. He has also served as a network administrator for some of the country's largest insurance companies and for the Department of Defense at Fort Knox. In addition to his continued work in IT, Posey has spent the last several years actively training as a commercial scientist-astronaut candidate in preparation to fly on a mission to study polar mesospheric clouds from space. You can follow his spaceflight training on his Web site.