Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a script where I mount with the command "New-PSDrive" a network drive. Now, since the script is running as a "cronjob" on a server I want to have some error detection. If for any reason the command New-PSDrive fails the script should stop executing and notify that something went wrong. I have the following code:

Try {
    New-PSDrive -Name A -PSProvider FileSystem -Root \\server\share
} Catch {
    ... handle error case ...
... other code ...

For testing reasons I specified a wrong server name and I get the following error "New-PSDrive : Drive root "\wrongserver\share" does not exist or it's not a folder". Which is OK since the server does not exists. But the script does not go into the Catch clause and stop. It happily continues to run and ends up in a mess since no drive is mounted :-)

So my question, why? Is there any difference in Exception handling in PowerShell? I should also note that I'm a noob in PowerShell scripting.

Bye, Martin

share|improve this question
up vote 1 down vote accepted

There are two types of errors in Powershell, terminating errors and non-terminating errors. Check out the help for about_try_catch_finally for more info.

If I try this

"It's a jimjim error!"

the nonsense cmdlet will generate a terminating error which will be caught by the catch block.

The code you are running is not throwing a terminating error, so execution throws the non-terminating error and continues after the catch block.

Also see this page,, for more information.

I am still a little fuzzy on when exactly a terminating error is thrown as opposed to a non-terminating error (perhaps more knowledgable folks can help out).

share|improve this answer
Yes, this is exactly right -- and it is sometimes difficult to know if/when a cmdlet is going to throw a terminating error or not. Which is why powershell gives you some options -- you can set the automatic variable $errorActionPreference to 'Stop' in your script and then ANY errors will be treated as terminating errors (and thus trigger the catch block). Alternately, and perhaps better in this specific scenario, specific cmdlets have the -ErrorAction paramter; just add "-ErrorAction Stop" to your New-PSDrive line and all errors from that specific cmdlet will be caught. – jbsmith Jul 26 '11 at 5:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.