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:
- Open PowerShell ISE as an administrator.
- 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 useful when you are working with SQL Server and you encounter an exception. When you run this command, you get the full stack trace; therefore, you get a more complete picture of exactly what went wrong. For example, when you run your...