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
"get-childitem c:\ -exclude windows" has no output #11649
"The implementation of -Include / -Exclude with Get-ChildItem is unintuitive and has pitfalls"
The command to do what you want is Get-Item c:\* -Exclude windows (rather than get-childitem).
The "get-childitem -exclude option" does filtering on the path you give to it, rather than the childitems it is about to output. You give it a path with a wildcard in it, it resolves the wildcard to a list of possibles, then includes/excludes from that set of wildcarded paths, then looks for childitems in the remaining set and outputs all of them. From this design it can't do what you ask. Get-ChildItem -Path c:\* -Exclude Windows will get you the child items of all the folders in c:\ excluding the childitems in c:\windows, or one level too deep. Compare with get-childitem c:\ -Exclude \ which gets you the child items of c:\ excluding anything in c:\ - i.e. nothing.
Even though it still seems like this should work for you, the SO answer says "In the special case of path C:\ (Get-ChildItem -Path C:\ -Exclude ), where arguably the last path component is the empty string, it seems that any exclusion pattern is considered a match, and no output is ever produced" - this may be a bug, but also may be a breaking change to fix it.
(The help get-childitem -online text is not very clear on explaining this behaviour of -Exclude)
By the way, specifying the path this way gives an error message:
get-childitem Microsoft.PowerShell.Core\FileSystem::C:\ -exclude windows
get-childitem : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
At line:1 char:1+get-childitem Microsoft.PowerShell.Core\FileSystem::C:\ -exclude wind ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.GetChildItemCommand
You'll get something similar in osx:
get-childitem/-directory -exclude varget-childitem : Cannot process argument because the value of argument "path" is not valid. Change the value of the "path" argument and run the operation again.
At line:1 char:1+get-childitem/-directory -exclude var+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Get-ChildItem], PSArgumentException
+ FullyQualifiedErrorId : Argument,Microsoft.PowerShell.Commands.GetChildItemCommand
By the way, I'm having trouble editing these posts in any browser.
Reacted by HumanEquivalentUnit and Patrick Meinecke
does filtering on the path you give to it, rather than the child items it is about to output.
It applies the filter on the given path first, and then to the child items - and that first application may prevent child items from getting considered at all. Situationally, it still happens to work as expected, as in jszabo98's get-childitem c:\windows -directory -exclude winsxs example: the -Exclude pattern doesn't exclude c:\windows itself, so the child items are considered, and WinSxS is properly excluded.
This counter-intuitive, but seemingly by-(unfortunate)-design behavior has previously been reported in #3304 (I previously got the above distinction wrong there as well, but have fixed it now).
I've made that clearer in the SO answer, and I've added a link to this issue to highlight the outright bug with root paths that jszabo98 discovered here (which the SO answer described inadequately - fixed now).
To summarize the bug:
Targeting a root directory with either Get-ChildItem or Get-Item in combination with either -Include or -Exclude - irrespective of the specific patterns passed - is broken, in the following scenarios:
On Windows without a provider-qualified path: the command is unexpectedly a quiet no-op
On Unix-like platforms without a provider-qualified and on Windows with a provider-qualified path: the command breaks with statement-terminating error cannot process argument because the value of argument "path" is not valid.
Curiously, the behavior is not broken with a provider-qualified path on Unix.
These tests revealed an unrelated bug on Windows, namely that Get-Item Microsoft.PowerShell.Core\FileSystem::\ and Get-Item Microsoft.PowerShell.Core\FileSystem::/ by themselves don't work (whereas Get-ChildItem does) - see #11660
I don't think that's related, because it is only true in Windows PowerShell, and looks like the old stringification problem of System.IO.FileSystemInfosituationally stringifying by file name only, and the file name of a root path is the empty string.
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commented on Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
Activity
HumanEquivalentUnit commentedon Jan 21, 2020
Michael Klement (@mklement0) has a very detailed discussion of this behaviour here in a StackOverflow answer but the short version would be:
Get-Item c:\* -Exclude windows(rather than get-childitem).Get-ChildItem -Path c:\* -Exclude Windowswill get you the child items of all the folders in c:\ excluding the childitems in c:\windows, or one level too deep. Compare withget-childitem c:\ -Exclude \which gets you the child items of c:\ excluding anything in c:\ - i.e. nothing.(The
help get-childitem -onlinetext is not very clear on explaining this behaviour of -Exclude)jszabo98 commentedon Jan 21, 2020
Excluding on the root directory seems to be a special case. On subdirectories it works fine.
By the way, specifying the path this way gives an error message:
You'll get something similar in osx:
By the way, I'm having trouble editing these posts in any browser.
jszabo98 commentedon Jan 22, 2020
This seems to be a workaround for ps 6 & 7:
mklement0 commentedon Jan 22, 2020
The simplest workaround is still the one quoted by HumanEquivalentUnit above:
Thanks for the summary, HumanEquivalentUnit - small correction:
It applies the filter on the given path first, and then to the child items - and that first application may prevent child items from getting considered at all. Situationally, it still happens to work as expected, as in jszabo98's
get-childitem c:\windows -directory -exclude winsxsexample: the-Excludepattern doesn't excludec:\windowsitself, so the child items are considered, andWinSxSis properly excluded.This counter-intuitive, but seemingly by-(unfortunate)-design behavior has previously been reported in #3304 (I previously got the above distinction wrong there as well, but have fixed it now).
I've made that clearer in the SO answer, and I've added a link to this issue to highlight the outright bug with root paths that jszabo98 discovered here (which the SO answer described inadequately - fixed now).
To summarize the bug:
Targeting a root directory with either
Get-ChildItemorGet-Itemin combination with either-Includeor-Exclude- irrespective of the specific patterns passed - is broken, in the following scenarios:On Windows without a provider-qualified path: the command is unexpectedly a quiet no-op
On Unix-like platforms without a provider-qualified and on Windows with a provider-qualified path: the command breaks with statement-terminating error
cannot process argument because the value of argument "path" is not valid.Curiously, the behavior is not broken with a provider-qualified path on Unix.
Here are Pester tests:
These tests revealed an unrelated bug on Windows, namely that
Get-Item Microsoft.PowerShell.Core\FileSystem::\andGet-Item Microsoft.PowerShell.Core\FileSystem::/by themselves don't work (whereasGet-ChildItemdoes) - see #11660jszabo98 commentedon Jan 22, 2020
This might be the cause. The string version of the parent properties returned is an empty string.
mklement0 commentedon Jan 22, 2020
I don't think that's related, because it is only true in Windows PowerShell, and looks like the old stringification problem of
System.IO.FileSystemInfosituationally stringifying by file name only, and the file name of a root path is the empty string.jszabo98 commentedon Jan 22, 2020
Oh I see, fileinfo's don't even have parent properties.
aguevara commentedon Sep 28, 2021
I just encountered this bug in my daily work, then discovered this issue on search. Any plans to fix it?
20-EverGreen-2 commentedon Jan 17, 2022
Any plans to fix via Windows update? I'm on 21H2 release. thx!
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 16, 2023
This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.
microsoft-github-policy-service commentedon Nov 23, 2023
This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.