GKalnytskyi GKalnytskyi - 1 year ago 256
PowerShell Question

Get only leaf nodes from Get-ChildItem

I would like to filter out only leaf nodes (folders) from

, those ones that do not contain any other folders in them.

Here is my current query:

Get-ChildItem -Recurse -Directory -Exclude "*SubStr*"

Answer Source

He was on the right path, but just forgot the -Directory on the second Get-ChildItem command.

Get-ChildItem -Recurse -Directory -Exclude "*SubStr*" | Where-Object { -not (Get-ChildItem $_.FullName -Directory) }

Note: If you want to find hidden folders you will have to use -Force on both Get-ChildItem commands in the line below.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download