Skip to content

Exact non-wildcard -Path target can be dropped by name filters during forced globbing (affects -Exclude, -File, -Attributes) #28466

Open

Description

@soroshsabz

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   # -> empty

Root cause chain:

  1. LocationGlobber.ShouldPerformGlobbing returns true when -Include/-Exclude is present, even if the path contains no wildcard characters.
  2. LocationGlobber.GenerateNewPSPathsWithGlobLeaf enumerates 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.
  3. 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

Activity

  1. mklement0 commented on Nov 22, 2026

    @mklement0
    Contributor

    It looks like you've pinpointed the root cause of several existing issues, the earliest dating back to 2017:

    While I do hope that your proposed fix will be green-lit, there is a conceptually related problem relating to the -Recurse switch, 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 -Recurse behavior.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions