Skip to content

Add -ExcludeDirectory dynamic parameter to the FileSystem provider for Get-ChildItem - #28465

Open
soroshsabz wants to merge 2 commits into
PowerShell:masterfrom
soroshsabz:exclude-directory
Open

soroshsabz wants to merge 2 commits into
PowerShell:masterfrom
soroshsabz:exclude-directory

Conversation

@soroshsabz

Copy link
Copy Markdown

PR Summary

Adds a new dynamic parameter -ExcludeDirectory to the FileSystem provider for Get-ChildItem, implementing the Working Group proposal in #15159 (specifically this WG decision).

-ExcludeDirectory accepts 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:

Get-ChildItem -Recurse -ExcludeDirectory node_modules, .git

PR Context

-Exclude/-Include only filter by item name at each recursion level, so Get-ChildItem -Recurse -Exclude node_modules still enumerates everything inside node_modules and 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:

  • The parameter is added to GetChildDynamicParameters, so it is available for both Get-ChildItem and Get-ChildItem -Name.
  • Matching uses the same case-insensitive wildcard semantics as -Exclude (WildcardOptions.IgnoreCase), and only applies to directories — files are never excluded by it.
  • Filtering happens in FileSystemProvider.Dir(), in both the output path and the recursion path, so excluded subtrees are genuinely not traversed (streaming, no wasted enumeration).
  • Because the engine's manual recursion for -Name/-Include/-Exclude (and wildcard path expansion) is also driven by the provider's GetChildNames, the exclusion consistently applies in those flows as well — e.g. Get-ChildItem -Recurse -Name -ExcludeDirectory .git and combinations with -Include/-Exclude/-File/-Directory/-Depth behave as expected.
  • The directory explicitly targeted by -Path/-LiteralPath itself is never excluded (only its children), consistent with how -Exclude treats the target item.

Fixes #15159

PR Checklist

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.
@soroshsabz
soroshsabz requested a review from a team as a code owner November 20, 2026 09:15
Copilot AI balanced review requested due to automatic review settings November 20, 2026 09:15
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

1 open issue
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

@doctordns

Thomas Lee (doctordns) commented Nov 20, 2026 •

Copy link
Copy Markdown
Collaborator

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)..

@soroshsabz

Copy link
Copy Markdown
Author

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).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues were identified for the directory-exclusion change.

No open issues 🎉

1 resolved since last review

Review effort: Balanced

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Feature Request: Could you please add a option to exclude a folder and also exclude its subfolders?

3 participants