Pages

Monday, August 22, 2011

Delete empty directories by using PowerShell script

It was interesting to create a script for finding empty folder under file share and delete with creating a log of directories.

##########################################################################
#Purpose of this script is to find blank folders from particular location
#and report into a text file and delete.
#Version - 1
#Auther - Prajeesh
##########################################################################

$date = ( get-date ).ToString('yyyyMMdd-HHmm')
$items = Get-ChildItem ‘\\Server1\Operations\2006’ -Recurse
foreach($item in $items)
{
      if( $item.PSIsContainer )
      {
            $subitems = Get-ChildItem -Recurse -Path $item.FullName
            if($subitems -eq $null)
            {
               $item.Fullname | Out-File -FilePath C:\Users\Prajeesh\Desktop\"EmtyFolder-$date.txt" -Append
                "Remove item: " + $item.FullName
                  Remove-Item $item.FullName
            }
      }
}

 

Just copy paste above script into a PowerShell Editor and edit locations [where to scan and where to store the log file] save file as ps1 and execute.

No comments:

Post a Comment