Skip to content

Get-ChildItem -Recurse with -Name does not recurse #9014

Closed

Description

Steps to reproduce

Get-ChildItem -Path *.txt -Recurse -Force -Name

In a folder with for example

S:\play\dir-demo> tree /f
Folder PATH listing for volume Source
Volume serial number is E68B-B87E
S:.
│   File.txt
│
├───sub1
│   │   File.txt
│   │
│   ├───subsub1
│   │       File.txt
│   │
│   └───subsub2
│           File.txt
│
├───sub2
│       File.txt
│
└───sub3
        File.txt

Expected behavior

Relative paths to all text files found in subdirectories of the current directory.

Actual behavior

S:\play\dir-demo> Get-ChildItem -Path *.txt -Recurse -Force -Name
File.txt

Environment data

Name                           Value
----                           -----
PSVersion                      6.1.3
PSEdition                      Core
GitCommitId                    6.1.3
OS                             Microsoft Windows 10.0.17763
Platform                       Win32NT
PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
PSRemotingProtocolVersion      2.3
SerializationVersion           1.1.0.1
WSManStackVersion              3.0

(also reproduces on inbox 5.1.17763.316)

Activity

  1. added
    Issue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get a
    on Feb 28, 2019
  2. mithrandir commented on Feb 28, 2019

    @mithrandir

    You can remove -path *.txt

    PS C:\Users\mithrandir\Documents\test> gci -r -name
    sub1
    sub2
    sub3
    File.txt
    sub1\File.txt
    sub2\File.txt
    sub3\File.txt
    PS C:\Users\mithrandir\Documents\test> gci -r -name -path *.txt
    File.txt
    
  3. SteveL-MSFT commented on Feb 28, 2019

    @SteveL-MSFT
    Member

    -Name should be able to use with -Path since they are not mutually exclusive. The problem appears to be here. Basically, when -Name is used, what's happening is that if the result is a directory, it will recurse into the results of matching with -Path. So maybe the fix here is in documentation. To get the behavior you want:

    Get-ChildItem -Recurse -Force -Name -Include *.txt

    If you want to specify a starting path to recurse:

    Get-ChildItem -Recurse -Force -Name -Include *.txt -Path c:\users\slee
  4. rainersigwald commented on Feb 28, 2019

    @rainersigwald
    Author

    What would change in the documentation?

    My understanding was that this

    Example 3: Get child items in the current directory and subdirectories

    This example displays .txt files that are located in the current directory and its
    subdirectories.

    PS> Get-ChildItem -Path C:\Test\*.txt -Recurse -Force
    ...
    

    The Get-ChildItem cmdlet uses the Path parameter to specify C:\Test\*.txt. Path uses the
    asterisk (*) wildcard to specify all files with the file name extension .txt. The Recurse
    parameter searches the Path directory its subdirectories, as shown in the Directory:
    headings. The Force parameter displays hidden files such as hiddenfile.txt that have a mode
    of h.

    combined with

    -Path

    Specifies a path to one or more locations. Wildcards are accepted. The default location is the
    current directory (.).

    implied that the pattern could be specified as the -Path parameter and would be recursed. I'm not sure I see a straightforward way to correct that (but maybe I'm just missing it).

  5. SteveL-MSFT commented on Feb 28, 2019

    @SteveL-MSFT
    Member

    I think the description of -Name in the documentation would be changed to reflect the current coded behavior which is when -Name is specified, the results of -Path filter is used for recursion. If you use a filter like -Path *.txt and there isn't a folder ending in .txt, no recursion will occur.

  6. mklement0 commented on Mar 3, 2019

    @mklement0
    Contributor

    Steve Lee (@SteveL-MSFT):

    So maybe the fix here is in documentation.

    I don't think that's the right fix for the baffling inconsistency of the following two commands not targeting the same set of files:

    # Finds *.txt files on all levels of the subtree.
    Get-ChildItem -Path *.txt -Recurse 
    
    # !! Finds *.txt files only as immediate children
    Get-ChildItem -Path *.txt -Recurse -Name

    Clearly, the intent of -Name is to only modify the type of the objects output - (relative) path strings instead of filesystem-info objects.
    That it changes what filesystem items are targeted should be considered a bug.

  7. SteveL-MSFT commented on Mar 11, 2019

    @SteveL-MSFT
    Member

    Michael Klement (@mklement0), I agree that the current behavior is not correct, however, the question is whether it meets our breaking change bar

  8. mklement0 commented on Mar 11, 2019

    @mklement0
    Contributor

    Understood, but given that the current behavior is both obscure and unhelpful, it's hard to imagine that anyone actually relies on it, which makes fixing this a Bucket 3: Unlikely Grey Area change.

  9. SteveL-MSFT commented on Mar 13, 2019

    @SteveL-MSFT
    Member

    @PowerShell/powershell-committee reviewed this, we don't believe this is a Bucket 3 item as it's likely users are relying on this in simple cases where they did not encounter the counter behavior. We agree that -Name should not change the behavior of Get-ChildItem -Recurse -Path *.txt outside of the output. However, due to the risks of the change, we propose that we should update the documentation to reflect the current behavior and pend any code change to a future FileSystemProvider v2 where -Name may not even be needed. If users want the expected behavior, use the pipeline.

    Related #5785

  10. added
    Committee-ReviewedPS-Committee has reviewed this and made a decision
    and removed
    Review - CommitteeThe PR/Issue needs a review from the PowerShell Committee
    on Mar 13, 2019
  11. 1 remaining item

  12. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  13. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  14. microsoft-github-policy-service commented on Nov 16, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has not had any activity in 6 months, if this is a bug please try to reproduce on the latest version of PowerShell and reopen a new issue and reference this issue if this is still a blocker for you.

  15. microsoft-github-policy-service commented on Nov 23, 2023

    @microsoft-github-policy-service
    Contributor

    This issue has been marked as "No Activity" as there has been no activity for 6 months. It has been closed for housekeeping purposes.

  16. removed their assignment
    on Sep 25, 2026
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

    Area-FileSystem-Providerspecific to the FileSystem providerCommittee-ReviewedPS-Committee has reviewed this and made a decisionIssue-Questionideally support can be provided via other mechanisms, but sometimes folks do open an issue to get aResolution-FixedThe issue is fixed.WG-Cmdletsgeneral cmdlet issues

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions