Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wildcards in the middle of a path don't work in Get-ChildItem with -Recurse and -File #21564

Open
5 tasks done
bugale opened this issue May 1, 2024 · 0 comments
Open
5 tasks done
Labels
Issue-Bug Issue has been identified as a bug in the product Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module

Comments

@bugale
Copy link
Contributor

bugale commented May 1, 2024

Prerequisites

Steps to reproduce

On a standard installation of Windows (with Powershell 7.4.2), running the following two commands:

Get-ChildItem -Path 'C:\Windows\System32\drivers\etc\*' -Recurse
Get-ChildItem -Path 'C:\Windows\System32\drivers\etc\*' -Recurse -File

results in the same output, for example on my machine:

    Directory: C:\Windows\System32\drivers\etc

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            6/5/2021  3:08 PM            824 hosts
-a---           4/25/2024  6:51 PM            439 hosts.ics
-a---            5/7/2022  8:22 AM           3683 lmhosts.sam
-a---            6/5/2021  3:08 PM            407 networks
-a---            6/5/2021  3:08 PM           1358 protocol
-a---            6/5/2021  3:08 PM          17635 services

If, on the other hand, a part of the path is replaced with an asterisk, the two commands return different output:

C:\Temp> Get-ChildItem -Path 'C:\Windows\System32\*rivers\etc\*' -Recurse -File
C:\Temp> Get-ChildItem -Path 'C:\Windows\System32\*rivers\etc\*' -Recurse

    Directory: C:\Windows\System32\drivers\etc

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            6/5/2021  3:08 PM            824 hosts
-a---           4/25/2024  6:51 PM            439 hosts.ics
-a---            5/7/2022  8:22 AM           3683 lmhosts.sam
-a---            6/5/2021  3:08 PM            407 networks
-a---            6/5/2021  3:08 PM           1358 protocol
-a---            6/5/2021  3:08 PM          17635 services

i.e. the command Get-ChildItem -Path 'C:\Windows\System32\*rivers\etc\*' -Recurse -File wrongfully returns nothing.
Note that passing only -Recurse without -File correctly returns all of the files in the etc directory.

Expected behavior

PS> Get-ChildItem -Path 'C:\Windows\System32\*rivers\etc\*' -Recurse -File

    Directory: C:\Windows\System32\drivers\etc

Mode                 LastWriteTime         Length Name
----                 -------------         ------ ----
-a---            6/5/2021  3:08 PM            824 hosts
-a---           4/25/2024  6:51 PM            439 hosts.ics
-a---            5/7/2022  8:22 AM           3683 lmhosts.sam
-a---            6/5/2021  3:08 PM            407 networks
-a---            6/5/2021  3:08 PM           1358 protocol
-a---            6/5/2021  3:08 PM          17635 services

Actual behavior

PS> Get-ChildItem -Path 'C:\Windows\System32\*rivers\etc\*' -Recurse -File

Error details

No response

Environment data

PS> 
Name                           Value
----                           -----
PSVersion                      7.4.2
PSEdition                      Core
GitCommitId                    7.4.2
OS                             Microsoft Windows 10.0.22631
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

Visuals

No response

Workaround

It's possible to workaround the issue by using | Where-Object { !$_.PSIsContainer } instead of -File

@bugale bugale added the Needs-Triage The issue is new and needs to be triaged by a work group. label May 1, 2024
@StevenBucher98 StevenBucher98 added WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module Issue-Bug Issue has been identified as a bug in the product Issue-Enhancement the issue is more of a feature request than a bug labels May 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Issue-Bug Issue has been identified as a bug in the product Issue-Enhancement the issue is more of a feature request than a bug Needs-Triage The issue is new and needs to be triaged by a work group. WG-Cmdlets-Management cmdlets in the Microsoft.PowerShell.Management module
Projects
None yet
Development

No branches or pull requests

2 participants