Techibee.com

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.

Exit mobile version