Tuesday 13 January 2009

Find largest folders on server

#Description: This powershell script allows you to search for folders containing largest files (over 100MB)
#Purpose: quickly find large files on remote server to resolve space issues
#Designed to be added into $profile for quick-admin tasks, run from commandline

#Tested with: Windows 2000, 2003, 2008
#Last edit by: MsMiller on 13/01/2009
$erroractionpreference = 'silentlycontinue'

$error.psbase.clear()
$erroractionpreference = 'silentlycontinue'

$startFolder = $args[0]
if ($args[0] -eq $null)
{
""
Write-Host -ForegroundColor green "This script scans a selected folder on your PC or network share looking for subfolders over 100MB"
""
Write-Host -ForegroundColor green "Your selected folder names can not contain spaces, so use quotes for your selection"
""
$startFolder = read-host "Enter share, folder you want to scan"
""
}
" "
"Start @ "
date -format G
" "
Write-Host -ForegroundColor green "Size of current directory excluding subfolders:"
" "
$colItems = (Get-ChildItem $startFolder | Measure-Object -property length -sum)
"$startFolder -- " + "{0:N3}" -f ($colItems.sum / 1GB) + " GB"
" "
Write-Host -ForegroundColor green "This might take few minutes depending on size of the chosen folder."
Write-Host -ForegroundColor green "You can exit the script with Ctrl+C and run it again narrowing down your selection."
" "
Write-Host -ForegroundColor green "Size of all subfolders over 100MB: ........"
" "
$colItems = (Get-ChildItem $startFolder -recurse | Where-Object {$_.PSIsContainer -eq $True} | Sort-Object)
foreach ($i in $colItems)
    {
        $subFolderItems = (Get-ChildItem $i.FullName | Measure-Object -property length -sum)
        $size="{0:N2}" -f ($subFolderItems.sum / 1GB)
       if ($size -gt 1)
  {
  $output=$i.FullName + " -- " + $size +  " GB"
  Write-Host -ForegroundColor red $output
  }
        if ($size -gt 0.1 -and $size -lt 1)
 {
 $output=$i.FullName + " -- " + $size +  " GB"
 $output
 }
   }
if ($output -eq $null)
                 {
    Write-Host -ForegroundColor yellow "There were no subfolders found with size over 100MB"
    }

" "
"End @ "
date -format G
" "