question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:open
  • Created 3 years ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
jdhitsolutionscommented, Aug 26, 2020

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:

PS C:\Windows> dir * -include u* -file


    Directory: C:\Windows

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---           8/26/2020 11:11 AM           5321 unsu.log

PS C:\Windows> dir * -include sys* -Directory


    Directory: C:\Windows\System32

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           8/13/2020  7:37 AM                Sysprep
d----           5/30/2020  2:00 PM                SystemResetPlatform

    Directory: C:\Windows\SysWOW64

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----           12/7/2019  4:50 AM                sysprep

PS C:\Windows>

But in further testing, this works in the root of C:

PS C:\> dir * -include p* -Directory


    Directory: C:\Pluralsight

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
da---            8/1/2017  1:17 PM                published-author-badge

    Directory: C:\Program Files

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
d----            4/1/2019  4:51 PM                PackageManagement
d----           7/30/2020 11:32 AM                Pale Moon
d----           7/17/2020  3:42 PM                PowerShell
d----            8/4/2020  2:49 PM                Private Internet Access
da---          10/11/2017 10:37 AM                PuTTY

But not this:

PS C:\> dir * -include u* -Directory

Even though there is a Users directory. I’m trying to figure out if this is a PowerShell or a Windows problem.

0reactions
mklement0commented, Feb 24, 2023

@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 contain a:

# !! Returns results such as "C:\Program Files\Application Verifier" on Windows.
Get-ChildItem /* -Include *a* -Directory | % FullName

It’s hard to imagine that anyone relies on this broken behavior.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found