Get-ChildItem -Include does not match directories (folders) when combined with -Directory
See original GitHub issue-Include and -Exclude operate on the leaf path components of the input path (pattern) only, even with Get-ChildItem (except if -Recurse is also specified) - while that is counterintuitive in and of itself, it has always worked this way (see #3304 and #8459).
Unexpectedly, however, -Include seems to match files only with Get-ChildItem (unlike with Get-Item), so combining
-Directory with -Include yields no output (except with * as the pattern):
Steps to reproduce
# Windows, macOS, Linux all have top-level u* folders
# The test would succeed with Get-Item (which requires removing -Directory);
# it also succeeds with -Filter instead of -Include
Get-ChildItem /* -Include u* -Directory | Should -Not -BeNull
Expected behavior
The test should pass.
Actual behavior
The test fails due to lack of output:
Expected a value, but got $null or empty.
Environment data
PowerShell Core 7.1.0-preview.6
Issue Analytics
- State:
- Created 3 years ago
- Reactions:1
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Recursive -notmatch in get-childitem with regex
Try this with a single call to Get-ChildItem and have it look for files. Then in the Where-Object clause, you filter on the...
Read more >Get-ChildItem - PowerShell
When a Get-ChildItem command includes the Depth or Recurse parameters, empty directories aren't included in the output. Locations are exposed to Get-ChildItem ......
Read more >Powershell Get-ChildItem Include\Exclude - simple script ...
The -Exclude parameter has never really worked properly. It seems to match on the Name property, which is typically not very useful.
Read more >Powershell Skip Folder using Get-ChildItem
Part of a PowerShell script I am running, I want it to completely ignore a specific folder or path. I've tried to use...
Read more >How to use PowerShell Get ChildItem Cmdlet
The PowerShell Get ChildItem cmdlet retrieves the child items from the specified location(s). The cmdlet is mainly used to retrieve files ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I’ve always found the parameters with Get-ChildItem a pain to teach and totally agree that I’d like to have consistency. But if I understand your issue, -Include fails with -Directory but works with -File. I can see this working in a folder:
But in further testing, this works in the root of C:
But not this:
Even though there is a Users directory. I’m trying to figure out if this is a PowerShell or a Windows problem.
@munrobasher, given that the current behavior is useless, I don’t think this would be a breaking change.
I just discovered that the behavior is even less predictably useless than I thought: With patterns such as
-Include *a*, you do get results, but seemingly only among subdirectories and only if both the immediate input directories and the subdirectories containa:It’s hard to imagine that anyone relies on this broken behavior.