Access denied exception is not catched RRS feed

  • Question

  • Within a for-each block, I use the following powershell code to analyze file paths:

      $path = "\\server\" + $fileName
        test-path -path $path;
        write-host "catched " $_.Exception

    If $path points to a path where I get an access denied exception, the result is as if there is no try-catch block, i.e.

    1. the exception is given in red and without the initial "catched " from my catch block and

    2. the result of test-path evaluation ("False") and of the following lines in the try block is also displayed.

    In order to check wether whatever is wrong with my try catch block, I've added the line with "$%variable.Length". This exception is caught always, i.e. I always see the output of my catch block.

    Can someone please explain and help?


    Thursday, July 17, 2014 6:33 AM


All replies

  • You cannot use test-path to check access to a file or folder.  It will only tell you if the folder exists.

    if(Test-Path $path){ 'Path exists'}

    If you try to open the file you will get an error.

    Get-Content $path

    That is where you need to check the error.


    Thursday, July 17, 2014 11:42 AM
  • try{
         Get-Content $path -EA stop


    • Proposed as answer by FZB Thursday, July 17, 2014 11:48 AM
    • Unproposed as answer by Stef_G Thursday, July 17, 2014 12:20 PM
    Thursday, July 17, 2014 11:44 AM
    • Marked as answer by Stef_G Thursday, July 17, 2014 12:20 PM
    Thursday, July 17, 2014 11:48 AM
  • I had been asking why the access denied exception generated by test-path is not handeled in a try catch block that handles exceptions produced by other commands.

    In other words, according to my experience, test-path does not (only) tell me wether a path (file / directory) exists but whether I have áccess to it. Actually, when I get the access denied error, the command is still evaluated to "True", which is correct as the path exists, which is not correct as this return value should not have been generated as the catch block should have handeled anything after the access denied exception ocurred.

    I hope my confusion and needs for clarification became clearer.

    Thursday, July 17, 2014 12:31 PM
  • Think about it. "Test-Path" only tests if the path exists.  It does not open the file.  It just checks the directory.  Only attempting to open the file will cause an access check.


    Thursday, July 17, 2014 12:35 PM
  • A picture says more than 1000 words.

    Please explain, based on your argument that only attempting to open the file causes an access check, the following output based on the input...

    Thursday, July 17, 2014 1:02 PM
  • Sory I cannot read that language.

    Are you sure you have access to the directory and you do not have corrupted security descritptors?

    I can test path to files I have no access to with no error.


    Thursday, July 17, 2014 1:34 PM
  • Sorry, the first line of the exception message is in German, it says

    Test-Path : Access denied In line:6 character:13

    Nevertheless, all the following lines are in English and, as I see it, confirm my translation.

    Based on your comments, it seems to me that I was able to communicate my confusion about this behavior ;-)

    Regardless whether you are correct, i.e. that test-path should not even cause an access denied exception, if this happens, for whatever reason, why is this exception not catched?   

    Thursday, July 17, 2014 1:54 PM
  • Cmdlets cannot be "caught" normally.  You must use the erroraction argument to stop the CmdLet which will cause it to "throw" and exception.

    Test-Path $path -ErrorAction Stop


    • Marked as answer by Stef_G Friday, July 18, 2014 6:12 AM
    • Unmarked as answer by Stef_G Friday, July 18, 2014 6:12 AM
    • Marked as answer by Stef_G Friday, July 18, 2014 6:13 AM
    Thursday, July 17, 2014 1:57 PM