Skip to content

System.IO.Directory.EnumerateDirectories Recurse enumeration with EnumerationOptions count is different as other tools #30699

@Kriegel

Description

@Kriegel

Hi!

I am like to enumerate Filesystem Directories with the .NET Classes and PowerShell 6 because they are faster than the Cmdlet Get-ChildItem.

My PowerShell Version is:

Name Value


PSVersion 6.2.2
PSEdition Core
GitCommitId 6.2.2
OS Microsoft Windows 10.0.16299
Platform Win32NT
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
WSManStackVersion 3.0

If I do a recurse enumeration over "C:\Windows" , with User permissions (to have a folder with lots of access denied) and the following Function under use of
System.IO.EnumerationOptions

The count of Directories is INCORRECT and shows a count of 35712
Missing 1057

Function Get-DirRecurseEnumerationOptions {

    [Cmdletbinding()]
    Param (
        [String[]]$Path
    )
    
    Begin {
         $EnumerationOptions = New-Object 'System.IO.EnumerationOptions'
         $EnumerationOptions.IgnoreInaccessible = $False
         $EnumerationOptions.RecurseSubdirectories = $False
    }


    Process {
        
        ForEach ($Dir in $Path) {
             [System.IO.DirectoryInfo]$Dir

            Try {
                ForEach ($D in [System.IO.Directory]::EnumerateDirectories($Dir,'*',$EnumerationOptions)) {
                    Get-DirRecurse -Path $D
                }
            } Catch {
                Write-Error $_
            }
        }
    }
}

Get-DirRecurseEnumerationOptions -Path 'C:\windows' -ErrorAction SilentlyContinue | measure

If I do the same with System.IO.SearchOption.TopDirectoryOnly i get the correct count of 36765


Function Get-DirRecurseSearchOption {

    [Cmdletbinding()]
    Param (
        [String[]]$Path
    )
    
    Process {
        
        ForEach ($Dir in $Path) {
             [System.IO.DirectoryInfo]$Dir

            Try {
                ForEach ($D in [System.IO.Directory]::EnumerateDirectories($Dir,'*',[System.IO.SearchOption]::TopDirectoryOnly)) {
                    Get-DirRecurse1 -Path $D
                }
            } Catch {
                Write-Error $_
            }
        }
    }
}

Get-DirRecurseSearchOption -Path 'C:\windows' -ErrorAction SilentlyContinue | measure

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.IOquestionAnswer questions and provide assistance, not an issue with source code or documentation.

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions