Repository navigation
Add -ExcludeDirectory dynamic parameter to the FileSystem provider for Get-ChildItem - #28465
soroshsabz wants to merge 2 commits into
Conversation
Implement the WG-approved proposal from PowerShell#15159: Get-ChildItem gains a new FileSystem provider dynamic parameter, -ExcludeDirectory, that takes an array of wildcard patterns. Directories whose names match any pattern are excluded from the output and, when recursing, are not recursed into, so their subtrees are never enumerated.
|
Azure Pipelines: There may be pipelines that require an authorized user to comment /azp run to run. |
There was a problem hiding this comment.
🟡 Changes recommended
The exclusion check can remove an explicitly targeted directory during path resolution, causing non-recursive Get-ChildItem calls combining -Exclude and -ExcludeDirectory to return nothing instead of the directory’s children.
What changed in this PR
Adds -ExcludeDirectory to the FileSystem provider for Get-ChildItem, allowing callers to omit matching directories and skip their subtrees, addressing #15159.
Changes:
- Accepts arrays of case-insensitive wildcard patterns that apply only to directories.
- Adds exclusion checks to child output and recursive traversal.
- Adds tests covering matching, parameter combinations, literal targets, and empty-pattern validation.
| File | Description |
|---|---|
test/powershell/Modules/Microsoft.PowerShell.Management/Get-ChildItem.Tests.ps1 |
Adds fixtures and tests for directory exclusion behavior. |
src/System.Management.Automation/namespaces/FileSystemProvider.cs |
Defines the dynamic parameter and filters matching directories from output and recursion. |
Review effort: Balanced
|
Why is this change needed. Why add all this extra functionality ? Is there really any demand for this (I have seen zero but thst msy be due to my poor Google-fu).. |
|
Thomas Lee (@doctordns) thanks for responding, as you can see in #15159, this requirement has a more than 15 upvotes; and WG confirmed |
…rectory Cover the target-accessibility cases discussed in PR review: an exact -Path target stays accessible when no other filters force globbing, and -LiteralPath keeps it accessible in combination with -Exclude. Document that a matching target name is itself filtered when other filters force globbing, consistent with -Exclude/-File semantics (issue PowerShell#28466).

PR Summary
Adds a new dynamic parameter
-ExcludeDirectoryto the FileSystem provider forGet-ChildItem, implementing the Working Group proposal in #15159 (specifically this WG decision).-ExcludeDirectoryaccepts an array of wildcard patterns. Directories whose names match any of the patterns are excluded from the output and are not recursed into, so their entire subtrees are skipped without being enumerated:PR Context
-Exclude/-Includeonly filter by item name at each recursion level, soGet-ChildItem -Recurse -Exclude node_modulesstill enumerates everything insidenode_modulesand only filters after the fact. There has been no way to prune whole directory subtrees up front, which is a common need (.git,.vs,node_modules, ...) and is what other tools (e.g. robocopy/XD) already offer.Per the WG decision, the feature is implemented as a dynamic parameter on the FileSystem provider, taking an array of wildcard patterns to exclude, where matching directories are also not recursed into.
Implementation notes:
GetChildDynamicParameters, so it is available for bothGet-ChildItemandGet-ChildItem -Name.-Exclude(WildcardOptions.IgnoreCase), and only applies to directories — files are never excluded by it.FileSystemProvider.Dir(), in both the output path and the recursion path, so excluded subtrees are genuinely not traversed (streaming, no wasted enumeration).-Name/-Include/-Exclude(and wildcard path expansion) is also driven by the provider'sGetChildNames, the exclusion consistently applies in those flows as well — e.g.Get-ChildItem -Recurse -Name -ExcludeDirectory .gitand combinations with-Include/-Exclude/-File/-Directory/-Depthbehave as expected.-Path/-LiteralPathitself is never excluded (only its children), consistent with how-Excludetreats the target item.Fixes #15159
PR Checklist
.h,.cpp,.cs,.ps1and.psm1files have the correct copyright header