PowerShell Genie

Find empty directories

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

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

1PS> $Root = "C:\Data"
2PS> Get-ChildItem $Root -Directory -Recurse -ErrorAction SilentlyContinue | Where-Object { -not (Get-ChildItem $_.FullName -Force -ErrorAction SilentlyContinue) } | Select-Object FullName

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 FullName
    2 --------
    3 C:\Sample\item.txt
    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
    1. Stage 1$RootPositional values or no named parameters
    2. Stage 2Where-ObjectPositional values or no named parameters
    3. Stage 3Select-ObjectPositional 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.

      1. Inspect the suspect folder with Get-ChildItem -Force to see what the test is actually finding in it.
      2. Drop -Force if hidden metadata should not count, then compare the new result count against the old one.
      3. Run the scan repeatedly until it returns nothing, since removing leaves turns their parents into new candidates.
      4. Add -Depth to bound the recursion when the tree is deeper than anyone expected.
      5. 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.

      Primary documentation

      Get-ChildItem reference

      Continue
      Genie guideFind empty directories

      Find empty folders with Get-ChildItem and Where-Object. Count hidden and system entries when deciding whether a directory is empty.