Cannot catch UnauthorizedAccessException RRS feed

  • General discussion

  • Hi All,

    I'm getting started with using try/catch/finally in powershell. I'm trying to use a multi-catch statement but I'm having trouble catching an UnauthorizedAccessException exception. The general catch statement will catch the exception but the cache [System.UnauthorizedAccessException] doesn't. I've done a lot of searching and have already adapted my function so that the ErrorAction is set to Stop in order to allow the exception to be caught. Here's my function:

    Function testExceptions {
            Write-Host "Attemting to copy file to c:\windows ..."
            Copy-Item C:\temp\test.txt c:\windows -ErrorAction Stop
        catch [System.UnauthorizedAccessException]
            Write-Host "Caught unauthorized access exception"
            Write-Host "Caught general exception"

    I'm running this from a powershell_ise.exe which is not elevated. The output is "Caught general exception".

    When I debug and have a breakline within the general catch block, I enter this command:

    $_ | Select-Object -Property *

    and get this output:

    [DBG]: PS C:\Windows\System32\WindowsPowerShell\v1.0>>> $_ | Select-Object -Property *

    PSMessageDetails      :
    Exception             : System.UnauthorizedAccessException: Access to the path 'C:\windows\test.txt' is denied.
                               at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
                               at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite)
                               at System.IO.FileInfo.CopyTo(String destFileName, Boolean overwrite)
                               at Microsoft.PowerShell.Commands.FileSystemProvider.CopyFileInfoItem(FileInfo file, String destinationPath,
                            Boolean force)
    TargetObject          : C:\temp\test.txt
    CategoryInfo          : PermissionDenied: (C:\temp\test.txt:FileInfo) [Copy-Item], UnauthorizedAccessException
    FullyQualifiedErrorId : CopyFileInfoItemUnauthorizedAccessError,Microsoft.PowerShell.Commands.CopyItemCommand
    ErrorDetails          :
    InvocationInfo        : System.Management.Automation.InvocationInfo
    PipelineIterationInfo : {0, 1}

    This is driving me nuts! Can someone please clarify why the catch [System.UnauthorizedAccessException] doesn't work?


    Wednesday, October 29, 2014 5:36 PM

All replies

  • I wish I had some good news for you, such that it's not working for me, but... while running as a normal, privileged user (that can't write to C:\Windows) it catches the error properly in the console and ISE. When I change the name of the file in the script, so that it's a non-existent file, it produces the general exception.

    Edit: As jrv pointed out, it works in PowerShell 4.0, which is what I'm using (on Windows 8.1).
    • Edited by tommymaynard Wednesday, October 29, 2014 10:12 PM
    Wednesday, October 29, 2014 6:54 PM
  • Well be carefully about using those long terms here.  Use "SEH" as it fits the page better.

    Yes - exceptions are hierarchical.  If you place a generic exception on the list it will likely trap first.

    Many of the exception are not called out correctly and there is not documentation for them. The callout in many cases uses a + or a ! to separate the class elemtn and detect it. I don't have a good example and I haven't seen any PosH Team blogs on this.  Post in the "Connect" forum to complain about this. If you search there you will find many complaints going back to PS V2.

    I know there has to be a simple way to work this out but havenet been able to track it down.

    Here is Shay Levy fielding the same question:


    Wednesday, October 29, 2014 8:42 PM
  • By the way...

    It works in V4.

    S C:\scripts> Get-host
    Name             : Windows PowerShell ISE Host
    Version          : 4.0
    InstanceId       : fc252990-493c-488e-96a1-edf288a8e785
    UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
    CurrentCulture   : en-US
    CurrentUICulture : en-US
    PrivateData      : Microsoft.PowerShell.Host.ISE.ISEOptions
    IsRunspacePushed : False
    Runspace         : System.Management.Automation.Runspaces.LocalRunspace
    PS C:\scripts> testExceptions
    Attemting to copy file to c:\windows ...
    Caught unauthorized access exception
    PS C:\scripts> 


    Wednesday, October 29, 2014 8:46 PM