Windows - Largest Disk Size Print

  • DiskSize
  • 0

Save the below code into a PowerShell file with exension .ps1

Say: Largest_File_Size.ps1

# PowerShell script to display files and directories with the largest size

# Function to format file size in human-readable format
function Get-HumanReadableSize($sizeInBytes) {
if ($sizeInBytes -gt 1GB) {
return "{0} GB" -f [math]::round($sizeInBytes / 1GB, 2)
}
elseif ($sizeInBytes -gt 1MB) {
return "{0} MB" -f [math]::round($sizeInBytes / 1MB, 2)
}
elseif ($sizeInBytes -gt 1KB) {
return "{0} KB" -f [math]::round($sizeInBytes / 1KB, 2)
}
else {
return "{0} Bytes" -f $sizeInBytes
}
}

# Get the top 10 largest files
$topFiles = Get-ChildItem -Recurse -File | Sort-Object Length -Descending | Select-Object -First 10

# Get the top 10 largest directories
$topDirs = Get-ChildItem -Recurse -Directory | Sort-Object { (Get-ChildItem $_.FullName -Recurse -File | Measure-Object -Sum Length).Sum } -Descending | Select-Object -First 10

# Display results
Write-Host "Top 10 Largest Files:"
foreach ($file in $topFiles) {
$size = Get-HumanReadableSize $file.Length
Write-Host "File > $($file.Name) > $size"
}

Write-Host ""
Write-Host "Top 10 Largest Directories:"
foreach ($dir in $topDirs) {
$dirSize = (Get-ChildItem $dir.FullName -Recurse -File | Measure-Object -Sum Length).Sum
$size = Get-HumanReadableSize $dirSize
Write-Host "Directory > $($dir.Name) > $size"
}

 

Open Powershell as administrator

Sample Output:

PS C:\Users\ryder> .\Largest_File_Size.ps1
Top 10 Largest Files:
File > BvSshClient-Inst.exe > 23.07 MB
File > captracking_comscanresults.zip > 4.23 MB
File > putty-64bit-0.83-installer.msi > 3.65 MB
File > touch-pouch.com-Feb-2023 > 3.23 MB
File > putty-64bit-0.76-installer.msi > 2.94 MB
File > mtputty_setup.exe > 2.47 MB
File > pidgin-data-ager.zip > 1.52 MB
File > tracking.com-Comodo_Online_Scanner.png > 1.32 MB
File > tracking.com_SiteLock_Malware_Scanning.png > 1.21 MB
File > tracking-com-SiteCheck-Using_Sucuri_Online_Scanner.png > 1008.88 KB

Top 10 Largest Directories:
Directory > Downloads > 52.7 MB
Directory > Documents > 10.99 MB
Directory > Daily_Tasks > 6.48 MB
Directory > Security Updates - C1 > 6.42 MB
Directory > pidgin-data-ager > 5.48 MB
Directory > pidgin-data-ager > 5.48 MB
Directory > .purple > 5.48 MB
Directory > jabber > 5.41 MB
Directory > logs > 5.41 MB
Directory > tracking_comscanresults > 4.87 MB
PS C:\Users\ryder>

 

-

Ryder

 


Was this answer helpful?

« Back