Link to home
Start Free TrialLog in
Avatar of ReneDK
ReneDKFlag for Denmark

asked on

PowerShell and catch exception

Hi,

this pretty simple piece of code is causing me a problem.

Why doesn't it catch the UnauthorizedAccessException

 try{ Get-ChildItem <path to folder with no access>}
catch{ "exception" }

I get the following error output:
Get-ChildItem : Access to the path '<path to folder with no access>' is denied
.
At line:1 char:19
+ try{ Get-ChildItem <<<<  <path to folder with no access>}catch{ "exception"
}
    + CategoryInfo          : PermissionDenied: (<path to folder with no access>:String) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.
   Commands.GetChildItemCommand
 
SOLUTION
Avatar of rlandquist
rlandquist
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneDK

ASKER

did you test that? because I already tried and it doesn't work for me...
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ReneDK

ASKER

Aaah... As I tried to hide the error output temporarily I added ErrorAction AND and the same time remove the try/catch. Didn't realize they had to be combined. Thanks a lot.