≡ Menu

PsTip# Find empty folders/Directories using Powershell

In today PSTIP section, I will show you how to find empty folder/directories in a disk/folder using powershell. It is a simple scripts that takes directory or Drive name as parameter and searches for empty folders inside that path. Currently this script is not deleting any empty folders but you can update it to remove them if you want. Let me know if any one needs help in that.

[cmdletbinding()]            
param(            
 [Parameter(Mandatory=$true)]            
 [string]$DirectoryName            
)            

$Directories = Get-ChildItem -Path $DirectoryName -Recurse | ? {$_.PSIsContainer -eq $true }            
foreach($Directory in $Directories) {            
 $Items = Get-ChildItem -Path $Directory.FullName            
 if(!$items) {            
  Write-Host "$($Directory.Fullname) is empty"            
 }            
}

Usage:

.\Get-EmptyFolders.ps1 -DirectoryName C:\

Hope this helps. Feedback welcome.

Comments on this entry are closed.

  • Icem@n February 19, 2013, 1:51 pm

    thx 😉
    for simplified version :
    Get-ChildItem -Recurse | ? {$_.PSIsContainer} | ? {!(Get-ChildItem $_)}

  • Mario July 3, 2013, 6:39 pm

    what parameters would u add to delete the empty folders?

    • Sitaram Pamarthi July 6, 2013, 6:10 pm

      You need to use a different cmdlet to delete the directories. Try Remove-Cmdlet -path

  • Almog July 23, 2013, 1:07 pm

    Hi ,
    first of all , 10x
    what parameters would u add to delete the empty folders?
    “Remove-Cmdlet-path”?? sorry i dont understand..

    thanks