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.
thx 😉
for simplified version :
Get-ChildItem -Recurse | ? {$_.PSIsContainer} | ? {!(Get-ChildItem $_)}
what parameters would u add to delete the empty folders?
You need to use a different cmdlet to delete the directories. Try Remove-Cmdlet -path
Hi ,
first of all , 10x
what parameters would u add to delete the empty folders?
“Remove-Cmdlet-path”?? sorry i dont understand..
thanks