Empty folders script #1

Sick of empty folders lying around making storage look messy? This script:

—————————————————————————————

new-psdrive -name someFolder -psprovider FileSystem -root “SomeDrive:\SomeFolder”

$foldersTotal = (Get-ChildItem “SomeDrive:\SomeFolder” -recurse -force | Where-Object {$_.PSIsContainer -eq $True})

foreach($folder in $foldersTotal)
{
 if($folder.GetFiles().Count -eq 0 -and $folder.GetDirectories().Count -eq 0)
 {$folder.Delete()}
}

remove-psdrive -name SomeFolder

—————————————————————————————

Recurses through any folder tree looking fomr completely empty folders. When it finds them, it deletes them. The catch? Depending on how badly “infested” storage is with empty folders, this can take ages and an unknown nubmer of iterations because it can only delete the lowest folder each time (if you have 3 empty folders- F1, F2 and F3 with F3 being the “lowest” in the tree, F3 is obviously the only completely empty folder as F2 ocntains F3, and F1 contains F2 and F3). So you might need to run it a lot, but it’s worth it. Once it stops finding empty folders, stick it in task scheduler to run every day (after hours, or else people might see their newly-creatd folders vanish before they’ve had a chance to put anything in it).

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s