0

I'm new to really using powershell, and just stumbled onto this. I can do

Get-ChildItem c:\windows -exclude Temp

and it works just fine. But running the same command on a root directory (e.g. c:\, d:\) will return nothing.

Get-ChildItem c:\ -exclude Temp

When I ran into this, I was baffled. At least one person has reported it as a "bug". My question is - is it really? Or is there some explanation for why this is "correct" behavior?

In searching for info on this problem, there is this where we find out -include is subtly different than -exclude in how it applies - though maybe it is at the root (no pun intended) of why the -exclude behavior is different for root directories?

I can work around the problem in several different ways. I was just hoping someone who understands powershell thoroughly might shed some light on if the behavior of -exclude on root directories really is a bug, or is somehow correct.

aggieNick02
  • 2,557
  • 2
  • 23
  • 36
  • 1
    `Exclude` has too many inconsistencies and I personally usually just filter with `Where-Object` – BenH Jun 12 '17 at 16:47
  • 1
    Just one of many misleading and untrustworthy PowerShell features, better get used to them now. For more fun, consider `Get-ChildItem C:\* -Exclude Temp` and see what that does. Then try `Get-Item C:\* -Exclude Temp` which probably does what you wanted Get-ChildItem to do. – TessellatingHeckler Jun 12 '17 at 18:32
  • @TessellatingHeckler - this is exactly what I was looking for! Thanks for the link and the samples of fun. I'll mark my q as a duplicate of the one you pointed to. – aggieNick02 Jun 12 '17 at 21:41

0 Answers0