Skip to content

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

Open

Description

@xylinkun

Summary of the new feature/enhancement

As a user I want PowerShell can exclude a folder and also exclude its subfolders and subfiles.

Proposed technical implementation details (optional)

For instance:

Get-ChildItem -Recurse -ExcludeFolderAndItsSubFolders "ExcludeFolder"

Then, the folder "ExcludeFolder" and all its subfolders and all files in it and its subfolders will be excluded.
"ExcludeFolderAndItsSubFolders " is only an example, it's a little long. Of course, you can choose any other better option name.

Activity

  1. added
    Issue-Enhancement
    Needs-Triage
    on Apr 5, 2021
  2. mklement0 commented on Apr 5, 2021

    @mklement0
    Contributor

    That would indeed be useful.

    There is a related idea in #4126 - though that isn't obvious from the wording, and the idea isn't fully fleshed out - which has been green-lit, but not implemented:
    It suggests making the existing -Include and -Exclude parameters accept paths rather than name-only patterns.

    That is, the syntax would be something like:

    Get-ChildItem -Recurse -Exclude ExcludeFolder/*

    However, that would only include ExcludeFolder and its children, not any more deeply nested items.

    To support something open-ended, we'd need something like ** (e.g., ExcludeFolder/**, as used in the Unix world) as a signifier that the entire subtree should be excluded - but that would be a departure from how PowerShell's wildcard patterns work.


    So perhaps the two ideas can be combined:

    • Allow path patterns, using the normal matching rules.

    • Additionally introduce -ExcludeRecursive (which I propose in lieu of your -ExcludeFolderAndItsSubFolders suggestion) and -IncludeRecursive parameters.

  3. 237dmitry commented on Apr 5, 2021

    @237dmitry

    In my opinion, this is unnecessary. This will complicate the cmdlet. This functionality is very easy to replace with -notmatch and -notlike operators. For example:

    (Get-Childitem -Recurse ./.local) -notlike '*powershell*'   # returns fullnames as string[]
    (Get-Childitem -Recurse ./.local) -notmatch 'powershell'    # returns FileInfo[], DirectoryInfo[]
    

    But I am not against the idea itself, but for wider functionality of -Exclude and -Include (regex)

  4. mklement0 commented on Apr 5, 2021

    @mklement0
    Contributor

    237dmitry:

    • While use of up-front collection of all output combined with operators is a workaround, it is not a satisfying solution, especially if you want to walk large hierarchies in a streaming fashion and if you want to exclude large subtrees to begin with (rather than enumerating them and excluding them after the fact).

    • The proposed new parameters may complicate the implementation, but they are easy to conceptualize and cover an important use case.

    • Certainly, regex patterns would give us more power and flexibility (at the expense of increased pattern-language complexity). However, given that PowerShell has no regex literals (it uses strings to represent regexes), also supporting regexes would require parallel parameters (e.g., -Exclude vs. -ExcludeRegex) - and that sounds like a problematic complication of the cmdlet.

  5. jborean93 commented on Apr 5, 2021

    @jborean93
    Collaborator

    Personally I agree with 237dmitry where this cmdlet is already complex enough and adding even more parameters is just going to make things worse. I don't see how having an -Exclude or -ExcludeRegex parameter would be beneficial over a simple | Where-Object Name -ne 'Blah', you can even use -match to match against a regex. The latter is highly customizable and already fit the existing PowerShell paradigms and providers don't support a way to natively exclude values in their underlying implementations so there are no native benefits by filtering that far left.

  6. mklement0 commented on Apr 5, 2021

    @mklement0
    Contributor

    Jordan Borean (@jborean93), I definitely agree that a -ExcludeRegex parameter is not called for - I used it to illustrate that it would be the only way to bring regex matching to Get-ChildItem and is therefore not a good idea.

    -Exclude and -Include are existing, wildcard-based parameters that currently:

    • work on an item's name only
    • at every level of the subtree being traversed with -Recurse.

    What this doesn't give you is a simple way to say "exclude all node_modules subfolders and their subtrees", and that is what this suggestion is about - and it seems worthwhile to me:

    • because it is a common use case

    • and something like -ExcludeRecursive node_modules is the most straightforward way to express this intent - no post-processing workarounds with additional patterns needed, and it also prevents unnecessary enumeration at the source.

  7. xylinkun commented on Apr 6, 2021

    @xylinkun
    Author

    That would indeed be useful.

    There is a related idea in #4126 - though that isn't obvious from the wording, and the idea isn't fully fleshed out - which has been green-lit, but not implemented:
    It suggests making the existing -Include and -Exclude parameters accept path rather than name-only patterns.

    That is, the syntax would be something like:

    Get-ChildItem -Recurse -Exclude ExcludeFolder/*

    However, that would only include ExcludeFolder and its children, not any more deeply nested items.

    To support something open-ended, we'd need something like ** (e.g., ExcludeFolder/**, as used in the Unix world) as a signifier that the entire subtree should be excluded - but that would be a departure from how PowerShell's wildcard patterns work.

    So perhaps the two ideas can be combined:

    • Allow path patterns, using the normal matching rules.
    • Additionally introduce -ExcludeRecursive (which I propose in lieu of your -ExcludeFolderAndItsSubFolders suggestion) and -IncludeRecursive parameters.

    Your understanding is absolutely what I want, Thank you.

  8. xylinkun commented on Apr 6, 2021

    @xylinkun
    Author

    In my opinion, this is unnecessary. This will complicate the cmdlet. This functionality is very easy to replace with -notmatch and -notlike operators. For example:

    (Get-Childitem -Recurse ./.local) -notlike '*powershell*'   # returns fullnames as string[]
    (Get-Childitem -Recurse ./.local) -notmatch 'powershell'    # returns FileInfo[], DirectoryInfo[]
    

    But I am not against the idea itself, but for wider functionality of -Exclude and -Include (regex)

    Suppose you are in a folder "PowerShell\blah\blah", your code will output nothing.

  9. 237dmitry commented on Apr 6, 2021

    @237dmitry

    Suppose you are in a folder "PowerShell\blah\blah", your code will output nothing.

    Suppose that you will create a specific regular expression for a specific case.

  10. xylinkun commented on Apr 6, 2021

    @xylinkun
    Author

    Suppose you are in a folder "PowerShell\blah\blah", your code will output nothing.

    Suppose that you will create a specific regular expression for a specific case.

    Create it, please.

  11. 237dmitry commented on Apr 6, 2021

    @237dmitry

    Create it, please.

    What subfolder have to be excluded? blah\powershell? What is the structure?

    <#
    Path\To\Powershell\2                        
    Path\To\Powershell\blah                     
    Path\To\Powershell\2\test                   
    Path\To\Powershell\blah\blah                
    Path\To\Powershell\blah\blah\powershell     
    Path\To\Powershell\blah\blah\powershell\test
    #>
    ((Get-Childitem PowerShell -Recurse) -notmatch '.+\\powershell.+powershell\\?').FullName
    <#
    Path\To\Powershell\2        
    Path\To\Powershell\blah     
    Path\To\Powershell\2\test   
    Path\To\Powershell\blah\blah
    #>
  12. xylinkun commented on Apr 6, 2021

    @xylinkun
    Author

    Create it, please.

    What subfolder have to be excluded? blah\powershell? What is the structure?

    <#
    Path\To\Powershell\2                        
    Path\To\Powershell\blah                     
    Path\To\Powershell\2\test                   
    Path\To\Powershell\blah\blah                
    Path\To\Powershell\blah\blah\powershell     
    Path\To\Powershell\blah\blah\powershell\test
    #>
    ((Get-Childitem PowerShell -Recurse) -notmatch '.+\\powershell.+powershell\\?').FullName
    <#
    Path\To\Powershell\2        
    Path\To\Powershell\blah     
    Path\To\Powershell\2\test   
    Path\To\Powershell\blah\blah
    #>

    Thank you. You are right. It's possible to implement it in your way.
    The reason for this demand is that I was writing a script recently that can format the filename in a directory but need to recursively exclude some directories, such as: .vs, .git, .github and etc. But I didn't find an easy way to achieve it.

  13. iSazonov commented on Apr 6, 2021

    @iSazonov
    Collaborator

    We could add a recurse predicate. De-facto we have it internally (for depth control) but we could have it as public parameter - a scriptblock.

  14. 19 remaining items

  15. SteveL-MSFT commented on Jul 2, 2025

    @SteveL-MSFT
    Member

    Jeff Hicks (@jdhitsolutions) and I discussed this, we both agree that supporting -Exclude folder/* syntax which is intuitive and existing in many other commands makes sense. Full support for pathspec seems too big an ask at this time and would be something PS engine itself would need to support. We don't have quorum to make formal decision.

  16. moved this from Queue to In-Progress-Issues in Cmdlets Working Groupon Jul 2, 2025
  17. CityguyUSA commented on Jul 3, 2025

    @CityguyUSA

    I'm just going to say this to say it. Powershell is a miss without fully trustible logic I can't use it. You, Microsoft, put forward that this is replacment for cmd but it's not. I know what cmd does and doesn't do. Here in Powershell world you dream about what Powershelll should do you pretend it does it but then it doesn't.

    I''m not blaming Steve Lee (@SteveL-MSFT) he's just saying it's too much for him to resolve. I'll take him at his word. I don't know the problems of Powershell and it's politics nor do I care to. But this is a major miss in my opinion without this it doesn't work. And just knowing that there's one thing that doesn't work I'm sure there are many more uncovered flaws that people like me are just taking MS at there word that it does work. But no more.

  18. TobiasPSP commented on Jul 3, 2025

    @TobiasPSP
    Collaborator

    I wonder why the option to submit a scriptblock (delegate) to a parameter (as alternative to string) isn’t considered.

    This is common behavior in many cmdlets (i.e. Rename-Item et al), is the beef in foreach/where-object (so it’s common practice) and provides ultimate power and flexibility, covering even the edgiest cases, without the need to inflate parameters.

    If -Include and - Exclude for example accepted scriptblocks as alternative to strings, with $_ exposing the item in question, anyone could compose any filtering behavior imho without breaking existing behavior nor adding any new parameter.

    Just a thought. Maybe I am missing something. It would certainly need to be implemented explicitly (in most cmdlets it is implicit by tagging the parameter with ValFromPipelineByProp, yet here the object in $_ would be internal and not coming from upstream).

  19. SteveL-MSFT commented on Jul 16, 2025

    @SteveL-MSFT
    Member

    The @PowerShell/wg-powershell-cmdlets discussed this. We agree on the utility and symmetry of excluding folders/subfolders. We looked at other existing tools like ls and robocopy. In the latter case, they have a similar parameter to exclude folders. So we propose to have a new dynamic parameter to FSProvider called -ExcludeDirectory which takes an array with wildcards to be excluded and also not recursed into.

  20. moved this from In-Progress-Issues to Reviewed in Cmdlets Working Groupon Jul 16, 2025
  21. added
    WG-Reviewed
    Up-for-Grabs
    and removed
    WG-NeedsReview
    on Jul 16, 2025
  22. CityguyUSA commented on Jul 16, 2025

    @CityguyUSA

    " and also not recursed into." This to me isis a huge part of the problem that you don't use recurse consistantly throughout the product. That's part 1. Part 2 suggests if you have thousands of files in mulptiple levels of directories you're expecting that the script writer includesa every one of those files by keying it. Seems absurd to me.

  23. TobiasPSP commented on Jul 16, 2025

    @TobiasPSP
    Collaborator

    Hm.

    The newly requested functionality is specifically for controlling recursive search, the suggested behavior is consistent with other tools.

    What do you mean by „keying in thousands of files“?
    And what would you want to work differently?

    Trying to understand your point ;-)

  24. CityguyUSA commented on Jul 17, 2025

    @CityguyUSA

    I ran into a problem the other week with path and recurse if my memory serves me it was the opposite of what I expected. It failed to provide the directory but provided the files within the directory. Somewhat recursive. I've had so many Powershell Issues that I can no longer trust what it says it does.

    Sepcifically on this issue, if I specify recurse then I should get every file and every directory from my starting point down and as much as I hate MS security it needs to adhere to a user's security to determine the data that will be included in the result set. That's how recurse has been defined over decades. All cmdlet's dealing with "items" that have security should also have a -user/-pw as common parameters that would allow you to override the basic user security. You obviously wouldn't want to store a clear text pw in a script but that's sabove my paygrade.

    Let's assume for the purposes of this arguement that I'm executing PowerShell as an administrator just to leave out the security piece. But let me say this about Aministrator; unless the ACL's have been setup to have the Administator's group having complete control of every directory, file and registry entry. I don't think your script will get you what you're expecting without having that ACL defined - that's my understanding. Even with the Administrator's Group ACL "properly defined" there are still exceptions which can cause a lot of confusion to which I have no official answer from MS explaining the issue.

    What I thought you were suggesting was to not have -recurse apply at all within the -include or -exclude parameters instead having those two items defaulting to -explicit. My first thought would be that in most cases you'd want to -exclude based on a file extension regardless of what directory the file resided in which would favor recurse.

    My second thought is considering the setup of Window's and it's roaming profiles you are most likely to encounter similar files in each user's roaming directory. Like c:\users*<-recurse excluding "All Users"<-recurse because you don't want anything within that directory and again with Default User, Public and other users as needed.

    To prevent future problems which willl surely come from essentially hard-coding this explicit restriction into the cmdlet would seem very short-sighted. Maybe the reason I got the werid result in my Get-ChildItem that I mentioned up front.

    First let's make sure that we have a common understanding of -exclude and -include. Unlike the path at the cmdlet level these are array type entries correct? I've not used either as yet but I'm going to base my answer on them being array entries. Being array entries make them a little more difficult to deal with using my solution because of how they are coded but not impossible.

    Each exclude/include directory entry should be specifically listed as recursive or explicit to get the best possible match to the directories and files the cmdlet will process. It shouldn't have to accept the cmdlet option because there's no relationship from the Get-ChildItem to any exclusions or inclusions. I'm not sure I really understand the include option. If you've already included it in your cmdlet path then you don't need to include it again. If it's not within your cmdlet path you should not encounter it to "include it". It's weird to add a path that you want included outside the cmdlet path it's like you're "anding" a second path into the cmdlet, something it wasn't designed to do and certainly could muck up or confuse someone examing it. Is there a case for -include?

    Going across all cmdlets -recurse needs to have the same comprehension as do exclude and include. The last thing you want is a different understanding based on the cmdlet.

    Without recursion on the exclusion you'd have to key each individual path and file who knows how many files we may be talking depends on what you're trying to accomplish.

    If inclusion is supposed to thwart exclusion I could see a value. If you exclude by recursion and want to include a single file or two or .... back in from the exclusion that might make sense.

  25. added
    In-PR
    on Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-ProviderIn-PRIssue-EnhancementKeepOpenUp-for-GrabsWG-CmdletsWG-Engine-ProvidersWG-Reviewed

    Type

    No type

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions