How to find empty directories in Windows using a Powershell Script

Easiest way I can think of is with a small PowerShell script. If you're running Windows 7 you should have it installed already, if not visit Microsoft.com to download and install it. The link provides a detailed description but the jist of the operation is included here for you convenience.

Open PowerShell and enter this:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFiles().Count -eq 0} | select FullName

Change C:\Scripts to whatever you want to search through, you can even set it to just C:\ if you want it to check the entire drive.

It will give you output like this (note these are the empty directories below C:\Scripts.

FullName
------- 
C:\Scripts\Empty 
C:\Scripts\Empty Folder 2 
C:\Scripts\Empty\Empty Subfolder 
C:\Scripts\New Folder\Empty Subfolder Three Levels Deep

If you look into PowerShell a bit I'm sure you'll be able to figure out how to automatically delete empty folders if you want to (though I recommend against it, just in case.)

Edit: As Richard mentioned in the comments, for a truly empty directory use:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName

The following is the easiest way I could find to achieve this with a single line of code. It lists the empty directories at the current location. If recursion is needed the parameter-Recurse could be added to the call to Get-ChildItem.

Get-ChildItem -Directory | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }

Short version with aliases:

dir -Directory | ? {$_.GetFileSystemInfos().Count -eq 0 }

Or, as a parameterized PowerShell function (I added this to my PowerShell startup profile):

Function Get-EmptyDirectories($basedir) { 
    Get-ChildItem -Directory $basedir | Where-Object { $_.GetFileSystemInfos().Count -eq 0 }
}

This can then be invoked as any other PowerShell function, including piping. For example, this call would delete all empty directories in the system temp directory:

Get-EmptyDirectories $env:TMP | del


Try this

Get-ChildItem C:\Scripts -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count}

The count is not 0, it doesn't exist at all meaning that the directory is completely empty or holds other completely empty folders