GKalnytskyi GKalnytskyi - 2 months ago 9
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*"


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.