What command do you use to find empty directories in Windows?

Some folders might contain some hidden folders like .svn or .settings, but they should still be treated as empty folders.


8 Answers 8


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 gist of the operation is included here for your 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.

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. It searches for hidden files and folders too:

(gci C:\Scripts -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName
  • am using Windows Vista
    – Joshua
    Aug 10, 2011 at 5:56
  • PowerShell for Vista can be downloaded here: microsoft.com/download/en/details.aspx?id=23200
    – Windos
    Aug 10, 2011 at 5:58
  • 10
    Note: that pipeline will find folders that contain no files but do contain other folders. To have completely empty folders change third element of the pipe to ?{$_.GetFileSystemInfos().Count -eq 0}.
    – Richard
    Aug 10, 2011 at 6:14
  • 1
    @Joshua, you didn't change "C:\Scripts" to where you want to search, hence you get the error "Cannot find path 'C:\Scripts' because it does not exist." It's right at the start of the line.
    – Windos
    Aug 10, 2011 at 9:35
  • 1
    @Joshua, have you read Richards comment (third one in this thread) and my edit to the answer? That should only give you directories with no files and no child directories (i.e. the leaf nodes)
    – Windos
    Aug 10, 2011 at 10:40

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

  • At least in powershell 5, symlinks/junctions i.e. with [System.IO.FileAttributes]:: ReparsePoint are returned by Get-ChildItem -Directory but they don't have method GetFileSystemInfos() causing an error. Filter those out with: $_.Attributes -eq [System.IO.FileAttributes]::Directory removes any entry with ReparsePoint flag enum set.
    – Carl Walsh
    Jun 15, 2023 at 0:15

Thanks, I used this as a basis for my script. I wanted to delete empty folders but trying to do Where-Object {$_.GetFiles().Count -eq 0} would delete folders that had sub-directories that were not empty. I ended up using a DO WHILE loop to remove a folder that had no files or folders then loop back and check again until it reached the end of the tree.

$Datefn=Get-Date -format M.d.yyyy_HH.mm.ss
#Set The File Name for the log file
$DelFileName = $Datefn
#Set The File Ext for the log file
$DelFileExt = " - Old Files" + ".log"
#Set The File Name With Ext for the log file
$DelFileName = $DelFileName + $DelFileExt
#Set Log Path
$LogPath = [Environment]::GetFolderPath("Desktop")
$Path = 'Q:\'
$NumDays = 365
Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
(Get-Date).addDays(-$NumDays) -and $_.psiscontainer -eq $false} |
    ForEach-Object  {
        $properties = @{`
            Path = $_.Directory`
            Name = $_.Name
            DateModified = $_.LastWriteTime
            Size = $_.Length / 1GB  }
    New-Object PSObject -Property $properties | select Path,Name,DateModified, Size
    } |
    Out-File "$LogPath\$DelFileName"
#Removes the files found
Get-ChildItem -Path $Path -Exclude DCID.txt,*.exe -Recurse | Where-Object {$_.lastwritetime -lt`
(Get-Date).addDays(-365) -and $_.psiscontainer -eq $false} | Remove-Item -Recurse -Force
#Removes empty folders
DO {
$a = (Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
{$_.GetFileSystemInfos().Count -eq 0} | Select-Object Fullname
(Get-ChildItem $Path -Recurse | Where-Object {$_.PSIsContainer -eq $true}) | Where-Object`
{$_.GetFileSystemInfos().Count -eq 0} | Remove-Item -Force
WHILE ($a -ne $null)

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


Combination of a couple answers above but if anyone needs to loop through the empty dirs

foreach($emptyDir in Get-ChildItem $outputdir -Recurse -Directory | Where-Object {!$_.GetFileSystemInfos().Count} | select FullName)
    Write-Host $emptyDir.FullName

In case you want to find and delete/remove the empty folders you can use this one-liner (a bit hardcore though... use with caution and good backups!). It will printout what folders it deleted. Sometimes a couple of empty folders are left after execution, and you have to run the one-liner several times to delete all empty folders.

(gci "E:\Backup P4\Bilder\Junkmappar" -r | ? {$_.PSIsContainer -eq $True}) | ?{$_.GetFileSystemInfos().Count -eq 0} | select FullName | ? {$_.fullname} | Remove-Item -path {$_.fullname} -force -verbose

...and of course replace my example path before running!


I think this calls for recursion...

Function Remove-EmptyPath
        [Parameter( Mandatory )]
        [String[]] $Path
        ForEach ( $LitPath in ( Resolve-Path $Path ))
            ForEach ( $subDir in ( Get-ChildItem -LiteralPath $LitPath -ad -Force ))
                Remove-EmptyPath $subDir.FullName
            If ( ! ( Get-Item -LiteralPath $LitPath -Force).GetFileSystemInfos() )
                Write-Verbose ( "Deleting directory: '$LitPath'" )
                Remove-Item -LiteralPath $LitPath -Force


Remove-EmptyPath . # Current directory
Remove-EmptyPath . -Verbose # List deleted direcotries


You can achieve this using GetFileSystemInfos + Select-Object with Format-Table (fix truncated output)

(gci "C:\Scripts" -r | ? {$_.PSIsContainer -eq $True}) | ? {$_.GetFileSystemInfos().Count -eq 0} | Select-Object FullName | Format-Table -AutoSize

Also use gci "" with quotation marks when specifying long filenames or paths with spaces

  • 1
    As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.
    – Community Bot
    Jun 27, 2023 at 18:11

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .