Skip to content

Get-ChildItem -Recurse treats literals passed to -Path like wildcard expressions #5699

@mklement0

Description

@mklement0

The 1st positional argument passed to Get-ChildItem binds to -Path, meaning that wildcard expressions are supported.

However, values that are literal paths - values that do not contain wildcard metacharacters such as * - should be treated as such.

  • Without -Recurse, that is already the case: A nonexistent literal path triggers an error.

  • With -Recurse:

    • The nonexistent path (assuming that it is a mere filename or its parent path exists) is treated like a wildcard expression that happens to match nothing.
    • Additionally, this matching is apparently performed in all the directories in the subtree.

Workaround (and generally the better choice if you know a path to be a literal one):
Get-ChildItem -Recurse -LiteralPath <path>

Update: Get-ChildItem -Recurse <name> may also yield false positives, by matching <name> anywhere in the subtree rather than just in the current location - see below.

Steps to reproduce

Get-ChildItem NoSuch
Get-ChildItem NoSuch -Recurse

Expected behavior

Both commands should report an error re nonexistent item NoSuch.

Actual behavior

Get-ChildItem NoSuch -Recurse produces no output at all (neither success nor error), and $? is set to $True.

Environment data

PowerShell Core v6.0.0-rc.2 (v6.0.0-rc.2) on Microsoft Windows 7 Enterprise  (64-bit; v6.1.7601)
Windows PowerShell v5.1.14409.1012 on Microsoft Windows 7 Enterprise  (64-bit; v6.1.7601)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Area-FileSystem-Providerspecific to the FileSystem providerBreaking-Changebreaking change that may affect usersCommittee-ReviewedPS-Committee has reviewed this and made a decisionResolution-No ActivityIssue has had no activity for 6 months or moreWG-Cmdlets-Managementcmdlets in the Microsoft.PowerShell.Management moduleWG-Engine-Providersbuilt-in PowerShell providers such as FileSystem, Certificates, Registry, etc.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions