Skip to content

Get-ChildItem ignores the -Depth parameter when -Include or -Exclude are specified #3726

Closed

Description

Steps to reproduce

Set-Location ~
Get-ChildItem . -Depth 0 | Measure-Object
Get-ChildItem . -Depth 0 -Include * | Measure-Object
Get-ChildItem . -Depth 0 -Exclude NoSuchItemImSure | Measure-Object

Expected behavior

All 3 commands should output the same number - the count of items in the current dir.

Actual behavior

  • The 1st command reports the number of items in the current directory only.

  • The 2nd and 3rd commands unexpectedly traverse the entire subtree, ignoring -Depth 0.

Environment data

PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on macOS 10.12.4
PowerShell Core v6.0.0-alpha (v6.0.0-alpha.18) on Microsoft Windows 10 Pro (64-bit; v10.0.14393)

Activity

  1. added
    Issue-Discussion
    on May 8, 2017
  2. Windos commented on Aug 30, 2017

    @Windos
    Contributor

    Given that I spent longer than I care to admit being tripped up by this this morning, I figure it couldn't hurt to add a comment on this issue in support of sorting it.

    To my mind I see two potential courses of action:

    1. Make Include/Exclude and Depth mutually exclusive. Not intuitive, but at least the user will get an error rather than unexpected results.
    2. Adjust the cmdlet so that using these combinations of parameters works as one would expect them to.

    I'd say the second one is more desirable, but I'm approaching this from a high-level as a user so I don't fully appreciate the amount of work or if it would result in any breaking changes.

  3. added
    Up-for-Grabs
    Issue-Bug
    and removed
    Issue-Discussion
    on Aug 30, 2017
  4. psmolkin commented on Sep 15, 2017

    @psmolkin

    The same behavior in 5.1.15063.608.
    Get-ChildItem -Depth 0 -Filter 'mask' works good, but
    Get-ChildItem -Depth 0 -Include 'mask' returns all 'mask' elemens of tree.

  5. added
    Hacktoberfest
    on Oct 1, 2017
  6. removed
    Up-for-Grabs
    on Dec 12, 2017
  7. jszabo98 commented on Apr 5, 2024

    @jszabo98

    This is still true to this day in powershell 5.1 (5.1.19041.4170).

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

    HacktoberfestIssue-BugResolution-FixedWG-Cmdlets

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions