Getting more error messages

In this recipe, we will learn how to display additional error messages.

How to do it...

Let's take a look at how to display additional error messages:

  1. Open PowerShell ISE as an administrator.
  2. Add the following script and run it:
    Clear-Host
    $error[0] | Format-List -Force

How it works...

PowerShell supports some variables called automatic variables that store the state information or environment information, such as arguments, events, user directories, profile information, or error codes. The $error is an automatic array variable that holds all the error objects that are encountered in your PowerShell session. To display the last error message, you can use the following code:

$error[0] | 
Format-List -Force

This method is particularly ...

Get SQL Server 2014 with PowerShell v5 Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.