Skip to content

Adding -Name to Get-ChildItem -Recurse quietly ignores directories it isn't permitted to list #9122

Closed

Description

The inability to list a child directory's contents due to lack of permission correctly results in a (non-terminating) error when using Get-ChildItem -Recurse.

Simply adding -Name to change from outputting filesystem-info objects to strings unexpectedly causes these errors to be suppressed.

Steps to reproduce (on Windows)

Run the following Pester test:

Describe "Get-ChildItem: unlistable-directory test" {
  BeforeAll {
    Push-Location TestDrive:/
    # Create a regular and an unlistable directory.
    $null = New-Item -Type Directory someDir, someUnlistableDir
    $acl = Get-Acl someUnlistableDir
    $denyRule = New-Object System.Security.AccessControl.FileSystemAccessRule $env:USERNAME, ListDirectory, Deny
    $acl.AddAccessRule($denyRule)
    Set-Acl someUnlistableDir $acl
  }
  AfterAll {
    # Make the unlistable directory listable again, so that removal of the temp. folder
    # underlying TestDrive: doesn't fail.
    $acl = Get-Acl someUnlistableDir
    $acl.RemoveAccessRuleSpecific($denyRule)
    Set-Acl someUnlistableDir $acl    
    Pop-Location
  }
  
  It "Get-ChildItem -Recurse complains about child directory that cannot be listed." {
    (Get-ChildItem -Recurse -ErrorVariable err 2>$null).Name | Should -BeExactly someDir, someUnlistableDir
    $err.Count | Should -Be 1
  }

  It "Get-ChildItem -Recurse -Name complains about child directory that cannot be listed." {
    Get-ChildItem -Recurse -Name -ErrorVariable err 2>$null | Should -BeExactly someDir, someUnlistableDir
    $err.Count | Should -Be 1
  }
  
}

Expected behavior

Both tests should pass.

Actual behavior

Executing script .....Tests.ps1

  Describing Get-ChildItem: unlistable-directory test
    [+] Get-ChildItem -Recurse complains about child directory that cannot be listed. 797ms
    [-] Get-ChildItem -Recurse -Name complains about child directory that cannot be listed. 161ms
      Expected 1, but got 0.
      27:     $err.Count | Should -Be 1

That is, adding -Name caused the inability to list the contents of someUnlistableDir to be quietly ignored.

Environment data

PowerShell Core 6.2.0-rc.1
Windows PowerShell v5.1.17134.590

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Mar 11, 2019
  2. iSazonov commented on Jan 30, 2020

    @iSazonov
    Collaborator

    Will look after #11602

  3. iSazonov commented on Feb 14, 2020

    @iSazonov
    Collaborator

    Michael Klement (@mklement0) The cmdlet returns folders on which it writes an error. Should it includes unaccessable folders to result list or no? I'd expect no (yes, it will be a breaking change) that looks like more native.

    Update: never mind - it is right behavior that we see in Explorer.

  4. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  5. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  6. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  7. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area-FileSystem-Providerspecific to the FileSystem providerIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdletsgeneral cmdlet issuesWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions