Repository navigation
Description
Prerequisites
- Search the existing issues for duplicates: none found for this exact behavior.
Summary
When Get-ChildItem (and other provider cmdlets) is given an exact, non-wildcard -Path together with -Include/-Exclude (or a provider dynamic name filter such as -File), the engine force-globs the path and resolves the target by enumerating the parent directory's child names and matching the target's name against the filters. As a result, the explicitly requested target itself can be filtered out of its own resolution, making the command return nothing — even though the user explicitly named the target.
Details
Repro (stock PowerShell 7.7.0, Windows; any recent version behaves the same):
$root = Join-Path $env:TEMP 'gciQuirk'
New-Item -ItemType Directory -Path "$root/node_modules/nested" -Force > $null
New-Item -ItemType File -Path "$root/node_modules/lib.js" -Force > $null
$target = Join-Path $root 'node_modules'
# 1) -Exclude pattern matching the TARGET's own name: returns nothing
Get-ChildItem -Path $target -Exclude 'node_modules' # -> empty (expected: children)
# 2) -File on an exact DIRECTORY target: returns nothing
Get-ChildItem -Path $target -Exclude '*.js' -File # -> empty (expected: 'nested')
# 3) new -ExcludeDirectory (PowerShell/PowerShell#28465) matching the target name: same
Get-ChildItem -Path $target -Exclude '*.js' -ExcludeDirectory node_modules # -> emptyRoot cause chain:
LocationGlobber.ShouldPerformGlobbingreturnstruewhen-Include/-Excludeis present, even if the path contains no wildcard characters.LocationGlobber.GenerateNewPSPathsWithGlobLeafenumerates the parent's child names even for a wildcard-free last leaf (StringContainsGlobCharacters(leafElement) || isLastLeaf), and every provider name filter that is active for the cmdlet (engine-Exclude, and FileSystem provider dynamic filters like-File/-Directory/-Attributes/-ExcludeDirectory) is applied to the target's own name during that resolution.- If the target name matches a filter, the target is dropped from the resolved path list and the command returns nothing.
By contrast, -LiteralPath (which sets SuppressWildcardExpansion) does not glob, so the target is always accessible:
Get-ChildItem -LiteralPath $target -Exclude '*.js' -File # -> works (filtered children)Suggestion
Distinguish target resolution from child enumeration: when the last path element is an exact (wildcard-free) name, the target's own name should not be subject to filters during resolution — filters should apply to its children once enumerated. This would make -Exclude, -File, -Directory, -Attributes, and the new -ExcludeDirectory behave intuitively for exact targets.
This is also relevant for the new -ExcludeDirectory parameter (#28465), which inherits this pre-existing behavior consistently with -Exclude/-File.
Related Source Pull Requests
- Add -ExcludeDirectory dynamic parameter to the FileSystem provider for Get-ChildItem #28465 (new
-ExcludeDirectoryparameter; follows the existing behavior)
Activity
Add regression tests for exactly targeted directories with -ExcludeDi…
mklement0 commentedon Nov 22, 2026
It looks like you've pinpointed the root cause of several existing issues, the earliest dating back to 2017:
-Fileto aGet-ChildItemcall with-Excludeoutputs nothing, if-Pathis used #20191While I do hope that your proposed fix will be green-lit, there is a conceptually related problem relating to the
-Recurseswitch, where - sadly - it was decided not to implement a fix due to backward compatibility concerns:If the latter's root cause turns out to be the same, care would (unfortunately) have to be taken to not change the
-Recursebehavior.