Skip to content

Instantly share code, notes, and snippets.

@amirkhan81
Created November 30, 2015 16:18
Show Gist options
  • Save amirkhan81/766fea440c21d5494062 to your computer and use it in GitHub Desktop.
Save amirkhan81/766fea440c21d5494062 to your computer and use it in GitHub Desktop.
Windows list large files
function Get-LowDisk($maxResults=15, $startLocation=(Get-Location)){
if("$startLocation".EndsWith("\")){$startLocation = "$startLocation".TrimEnd("\")}
function Calc-Directories ($startFolder, $foldersHT=@{}, $depth=1){ $itemSum = 0L;
if($startFolder -notmatch "c\:\\Windows|System Volume Information"){
$dirList = Get-ChildItem ($startFolder) -force
$folders = $dirList | Where-Object {$_.PSIsContainer -eq $True} | Where {($_.attributes.toString() -like "*ReparsePoint*") -eq $False};
$files = $dirList | Where-Object {!$_.PSIsContainer -eq $True}; if($files){$itemSum = ($files | Measure-Object -property length -sum).sum;}
if(!$folders.Count -and $folders -ne $null){
$subFolder = "$startFolder\" + $folders;
$itemSum += Calc-Directories "$subFolder" $foldersHT ($depth+1);
}else{
for ($i=0;$i -lt $folders.Count; $i++ ){
$subFolder = "$startFolder\" + $folders[$i].Name;
if($depth -le 1){ Write-Progress -Activity "Finding Largest Folders" -status ":: Top Level Folders Progress :: $subFolder"-percentComplete ($i / $folders.count*100) -Id 1;}
$itemSum += Calc-Directories "$subFolder" $foldersHT ($depth+1);
} }
}elseif($startFolder -match "c\:\\Windows"){$itemsum=(Get-ChildItem C:\Windows -force -Recurse -ErrorAction SilentlyContinue | Measure-Object -property length -sum).sum;}
$foldersHT[$startFolder] = $itemSum;
return $itemSum;
}
$elapsed = [System.Diagnostics.Stopwatch]::StartNew();
$resultsHT = @{}; Calc-Directories $startLocation $resultsHT | Out-Null;
$formattedOutput = $resultsHT.GetEnumerator() | sort value -desc | select -First $maxResults | Format-Table -AutoSize @{Expression={$_.Name};Label="Folder Name"}, @{Expression={($_.Value / 1GB)};Label="Size (GB)";Format="{0:N2}"};
write-host "`r`n`r`n-- Total Disk Scan Time: $($elapsed.Elapsed.ToString()) --";
write-host "`r`n-----------`r`nTop $maxResults folders by size`r`n-----------" -foregroundcolor "yellow";
return $formattedOutput;
} Get-LowDisk 15 ;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment