Error Handling
PowerShell

It is wise to build in some error handling in your scripts so that you know where to look when things go wrong.
A nice one is the variable !$?
Every time an error occurs the array $error is being written. You can read the last error because it is always stored in [0]

$error[0]

Will give you the last error that occurred.

You can also read the variable !$?

If (!$?) { "There is an error!"; break }; "Congratulations, everything is okay"

To find and colourize an error type the following:

Trap { Write-Host -Fore Red -back White $_.Exception.Message; Continue }; 1/$null
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License