Powershell: Get-ChildItem ignores the -Depth parameter when -Include or -Exclude are specified

Created on 7 May 2017  路  2Comments  路  Source: PowerShell/PowerShell

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)
Area-Cmdlets-Management Hacktoberfest Issue-Bug Resolution-Fixed

Most helpful comment

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.

All 2 comments

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.

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.

Was this page helpful?
0 / 5 - 0 ratings