Find empty directories
Documentation: Microsoft Learn
Find empty folders with Get-ChildItem and Where-Object. Count hidden and system entries when deciding whether a directory is empty.
The script
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
Inspect this example’s structure
Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.
$Root = "C:\Data"
Get-ChildItem $Root -Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { -not (Get-ChildItem $_.FullName -Force -ErrorAction SilentlyContinue) } | Select-Object FullName- Stage 1$Root
Positional values or no named parameters - Stage 2Where-Object
Positional values or no named parameters - Stage 3Select-Object
Positional values or no named parameters
How it works
The outer Get-ChildItem walks $Root for directories only. For each one, the Where-Object script block runs a second Get-ChildItem against its FullName with -Force so hidden and system entries still count, and the directory passes through only when that inner call returns nothing.
Adapting it
Removing -Force from the inner call treats folders containing only hidden files as empty, which matches what most migration tools assume and will lengthen the result list considerably.
If the result is unexpected
Directories that look empty in Explorer are not returned, or the scan takes far longer than the size of the tree suggests it should.
The inner test uses -Force, so hidden and system files count as contents and a folder holding nothing but a desktop.ini is reported as populated. Nested empty directories count as contents too, which excludes their parents. Every directory triggers its own child enumeration, so total work scales with folder count rather than file count.
- Inspect the suspect folder with Get-ChildItem -Force to see what the test is actually finding in it.
- Drop -Force if hidden metadata should not count, then compare the new result count against the old one.
- Run the scan repeatedly until it returns nothing, since removing leaves turns their parents into new candidates.
- Add -Depth to bound the recursion when the tree is deeper than anyone expected.
- Watch for reparse points, because following a junction can send the walk somewhere unrelated or into a loop.
Validate the result
A folder you know contains only hidden metadata appears in the output once -Force is removed, and a second pass after cleanup surfaces no new candidates.
Keep the check reliable
Decide up front whether hidden files count as contents and record that choice in the script, because the two behaviors produce very different lists.