You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Feature Request: Could you please add a option to exclude a folder and also exclude its subfolders? #15159
Area-FileSystem-Providerspecific to the FileSystem providerIn-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.WG-ReviewedA Working Group has reviewed this and made a recommendation
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.
Reacted by Michael Klement, Dongbo Wang, akapoor20, zett42, Santiago Squarzon, Julien, lzz110, Shayan Toqraee, Warzinski, Redbot and 7 more
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.
Reacted by Rain Sallow (/u/ta11ow), Kun Lin, Santiago Squarzon and Jan Sustr
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:
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.
Reacted by Kun Lin, Travis C. LaGrone, 19aliv70, Mavaddat Javid and Jade
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.
Reacted by Shayan Toqraee, DerSchinken, Jade and Przemysław Szypowicz
Reacted by Travis C. LaGrone, Shayan Toqraee and Przemysław Szypowicz
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.
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.
Reacted by Michael Klement, AlfiCode, Jan Sustr, Jade, Zachary Moote and Przemysław Szypowicz
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:
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.
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.
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.
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).
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.
" 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.
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.
Area-FileSystem-Providerspecific to the FileSystem providerIn-PRIndicates that a PR is out for the issueIssue-Enhancementthe issue is more of a feature request than a bugKeepOpenThe bot will ignore these and not auto-closeUp-for-GrabsUp-for-grabs issues are not high priorities, and may be opportunities for external contributorsWG-Cmdletsgeneral cmdlet issuesWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.WG-ReviewedA Working Group has reviewed this and made a recommendation
Activity
mklement0 commentedon Apr 5, 2021
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
-Includeand-Excludeparameters accept paths rather than name-only patterns.That is, the syntax would be something like:
However, that would only include
ExcludeFolderand 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-ExcludeFolderAndItsSubFolderssuggestion) and-IncludeRecursiveparameters.237dmitry commentedon Apr 5, 2021
In my opinion, this is unnecessary. This will complicate the cmdlet. This functionality is very easy to replace with
-notmatchand-notlikeoperators. For example:But I am not against the idea itself, but for wider functionality of
-Excludeand-Include(regex)mklement0 commentedon Apr 5, 2021
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.,
-Excludevs.-ExcludeRegex) - and that sounds like a problematic complication of the cmdlet.jborean93 commentedon Apr 5, 2021
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
-Excludeor-ExcludeRegexparameter would be beneficial over a simple| Where-Object Name -ne 'Blah', you can even use-matchto 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.mklement0 commentedon Apr 5, 2021
Jordan Borean (@jborean93), I definitely agree that a
-ExcludeRegexparameter is not called for - I used it to illustrate that it would be the only way to bring regex matching toGet-ChildItemand is therefore not a good idea.-Excludeand-Includeare existing, wildcard-based parameters that currently:-Recurse.What this doesn't give you is a simple way to say "exclude all
node_modulessubfolders 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_modulesis 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.xylinkun commentedon Apr 6, 2021
Your understanding is absolutely what I want, Thank you.
xylinkun commentedon Apr 6, 2021
Suppose you are in a folder "PowerShell\blah\blah", your code will output nothing.
237dmitry commentedon Apr 6, 2021
Suppose that you will create a specific regular expression for a specific case.
xylinkun commentedon Apr 6, 2021
Create it, please.
237dmitry commentedon Apr 6, 2021
What subfolder have to be excluded?
blah\powershell? What is the structure?xylinkun commentedon Apr 6, 2021
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.
iSazonov commentedon Apr 6, 2021
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.
19 remaining items
SteveL-MSFT commentedon Jul 2, 2025
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 forpathspecseems 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.CityguyUSA commentedon Jul 3, 2025
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.
TobiasPSP commentedon Jul 3, 2025
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).
SteveL-MSFT commentedon Jul 16, 2025
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
-ExcludeDirectorywhich takes an array with wildcards to be excluded and also not recursed into.CityguyUSA commentedon Jul 16, 2025
" 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.
TobiasPSP commentedon Jul 16, 2025
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 ;-)
CityguyUSA commentedon Jul 17, 2025
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.