How it works...
Errors can be broadly classified into two kinds: terminating and non-terminating errors. In this recipe, we used the try–catch construct to handle errors and actions related to them. The catch statement executes in case of errors in the try block, but only if the error in the try block is terminating. In our situation, though, the error returned upon not finding that the directory is a non-terminating error. For the catch block to be executed, we need to make the error a terminating one.
There are two ways to handle this. Either set the $ErrorActionPreference variable to Stop, or explicitly make errors generated by the New-Item statement terminate. The latter approach is recommended (and used in this recipe,) since that gives ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access