Get-Childitem exits with "Access is Denied" despite -ea 0

Welcome Forums General PowerShell Q&A Get-Childitem exits with "Access is Denied" despite -ea 0

This topic contains 3 replies, has 3 voices, and was last updated by

4 years, 4 months ago.

  • Author
  • #19409

    Points: 0
    Rank: Member

    Hi there,
    I'm running this line to search all ISO files on all drives:
    (GET-WMIOBJECT -query "SELECT * from win32_logicaldisk where DriveType = '3'").DeviceID| %{Get-ChildItem -path $_ -Include *.ISO -Recurse -Force -ea 0 |Select-Object FullName}

    It runs through some drives and list the ISOs that it finds along the way but then exits with this error:

    Get-ChildItem : Access is denied
    At line:1 char:92
    + ... ").DeviceID| %{Get-ChildItem -path $_ -Include *.ISO -Recurse -Force -ea 0 |Sele ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo : NotSpecified: (:) [Get-ChildItem], UnauthorizedAccessException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.GetChildItemCommand

    If I expand the error:

    PS C:\> $error[0]|format-list -force

    Exception : System.UnauthorizedAccessException: Access to the path 'P:\System Volume Information' is
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
    at System.IO.FileSystemEnumerableIterator`1.CommonInit()
    at System.IO.FileSystemEnumerableIterator`1..ctor(String path, String originalUserPath,
    String searchPattern, SearchOption searchOption, SearchResultHandler`1 resultHandler, Boolean
    at System.IO.FileSystemEnumerableFactory.CreateDirectoryInfoIterator(String path, String
    originalUserPath, String searchPattern, SearchOption searchOption)
    at Microsoft.PowerShell.Commands.FileSystemProvider.Dir(DirectoryInfo directory, Boolean
    recurse, Boolean nameOnly, ReturnContainers returnContainers)
    TargetObject : P:\System Volume Information
    CategoryInfo : PermissionDenied: (P:\System Volume Information:String) [Get-ChildItem],
    FullyQualifiedErrorId : DirUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetChildItemCommand
    ErrorDetails :
    InvocationInfo : System.Management.Automation.InvocationInfo
    ScriptStackTrace : at , : line 1
    at , : line 1
    PipelineIterationInfo : {0, 1, 0}
    PSMessageDetails :

    I can see that I'm not allowed access to "P:\System Volume Information" which I'd expect, but I also expect the ErrorAction of SilentlyContinue to sail past this problem. Any idea why this happens?

  • #19412

    Points: 1,811
    Helping HandTeam Member
    Rank: Community Hero

    So, first of all, please use "-EA SilentlyContinue" not "-EA 0." The whole point of the enumeration is to make your code easier to read ;).

    Second, -EA only controls what will happen in the event of a non-terminating error. Terminating errors always bail, and you can't stop it, but you can catch it in a Try/Catch construct. In this case, the cmdlet is throwing a terminating error, I'm guessing. That's under the control of whoever wrote the cmdlet and the underlying technology – you can't change that.

  • #19415

    Points: 0
    Rank: Member

    Great stuff Don, didn't know about the terminating error.
    Thanks for your help!

  • #19417

    Points: 24
    Team Member
    Rank: Member

    Interesting if I run your line on a computer with WMF 5.0 (September Preview) it works as you've expected earlier. Looks like the PowerShell team has changed the behaviour Get-Childitem cmdlet in combination with the -Force switch.

    I agree with Don, until we can get our hands on the final of WMF 5.0 bits you'll need to use the Try/Catch construct.


The topic ‘Get-Childitem exits with "Access is Denied" despite -ea 0’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort