Skip to content

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

@mklement0

Description

@mklement0

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

Metadata

Metadata

Assignees

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-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management moduleWG-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