Update – 04.06.2021 – I have an updated version of this script that is a little more flexible and has better output, you can find the new script here:
/2021/04/powershell-script-quickly-find-the-largest-files/
This is a super easy to use and flexible PowerShell script to find out what the largest files and folders are on a single drive. I find this script particularly useful in situations where you have a lot of servers that are being monitored for disk space and you need to quickly and easily find the largest offenders but take all of that information and put it in a readable format. That is why I call this my “low disk space” script. It is also nice to be able to pull this off without installing any software.
Here is the basic flow of the script:
- Scan the drive for total space free and used
- Scan for the largest files
- List the size of all of the folders on the root dir
- Interactive scan of specific sub folders
Notes: As always with PowerShell, you will need to set the execution policy for your environment accordingly. There are variables at the top of the script that can be edited to change various settings like the drive, extensions, file size, amount of files to list (ie top 25) and so on.
Instructions:
- Save to local machine as .ps1
- Open PowerShell as administrator
- Set your execution policy in PowerShell accordingly
- Change directory to where the script is and running it by typing .\FindLargestFilesAndFolders.ps1
Script download:
FindLargestFilesAndFolders.txt
Source:
###################################################################### # # This script will find the 10 (default) largest files and then list the # folders sizes of the c:\ drive (default). The results will be written to a # formatted text file. At the end of the script is an option to scan and list # the size of all sub folders within a particular folder, output will be # appended to the txt file generated during the initial scan. # # Author: Rob Willis 6/15/2016 # ###################################################################### # Modify these 3 variables as needed # Output file location and name $findFilesFoldersOutput = "C:\FindLargestFilesAndFolders.txt"; # Drive to Scan $diskDrive = "C:" # Limit number of rows on output, top XX files $filesLimit = 10; ################################### # Do not edit below! ################################### # Misc settings # Root location to scan for folder size $filesLocation = "$diskDrive\"; $rootLocation = $filesLocation; # Set output width $outputWidth = 150; # Minimum file size $fileSize = 10MB; # Search for specific file extensions - Default *.* $extension = "*.*"; # Time stamp $Time = Get-Date -format "dd-MMM-yyyy-HH-mm" ################################### # Scan Drive for capacity and free space ################################### Function driveScan { $disk = Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='$diskDrive'" | Select-Object Size,FreeSpace $total = "{0:N0}" -f ($disk.Size / 1GB) + " GB" $free = "{0:N0}" -f ($disk.FreeSpace / 1GB) + " GB" $freeMB = "{0:N0}" -f ($disk.FreeSpace / 1MB) + " MB" Write-Host " " write-host "Total capacity of $diskDrive - $total" write-host "Total space free on $diskDrive - $free / $freeMb" " " | out-file $findFilesFoldersOutput -append "Total capacity of $diskDrive - $total" | out-file $findFilesFoldersOutput -append "Total space free on $diskDrive - $free / $freeMb" | out-file $findFilesFoldersOutput -append " " | out-file $findFilesFoldersOutput -append " " | out-file $findFilesFoldersOutput -append } ################################### # Top Largest Files Scan ################################### Function fileScan { Write-Host " " Write-Host "Scanning $filesLocation for the $filesLimit largest files, this process will take a few minutes..." "Below are the $filesLimit largest files on $filesLocation from largest to smallest:" | out-file -width $outputWidth $findFilesFoldersOutput -append $largeSizefiles = get-ChildItem -path $filesLocation -include $Extension -recurse -ErrorAction "SilentlyContinue" | ? { $_.GetType().Name -eq "FileInfo" } | where-Object {$_.Length -gt $fileSize} | sort-Object -property length -Descending | Select-Object Name, @{Name="Size In MB";Expression={ "{0:N0}" -f ($_.Length / 1MB)}},@{Name="LastWriteTime";Expression={$_.LastWriteTime}},@{Name="Path";Expression={$_.directory}} -first $filesLimit $largeSizefiles | format-list -property Name,"Size In MB",Path,LastWriteTime | out-file -width $outputWidth $findFilesFoldersOutput -append Write-Host " " Write-Host "File Scan Complete..." Write-Host " " } ################################### # Top Largest Folders Scan ################################### Function folderScan { $subDirectories = Get-ChildItem $rootLocation | Where-Object{($_.PSIsContainer)} | foreach-object{$_.Name} Write-Host "Calculating folder sizes for $rootLocation," Write-Host "this process will take a few minutes..." "Estimated subfolder sizes for $rootLocation :" | out-file -width $outputWidth $findFilesFoldersOutput -append Write-Host " " " " | out-file -width $outputWidth $findFilesFoldersOutput -append $folderOutputFixed = @{} foreach ($i in $subDirectories) { $targetDir = $rootLocation + $i $folderSize = (Get-ChildItem $targetDir -Recurse -Force | Measure-Object -Property Length -Sum).Sum 2> $null $folderSizeComplete = "{0:N0}" -f ($folderSize / 1MB) + "MB" $folderOutputFixed.Add("$targetDir" , "$folderSizeComplete") write-host " Calculating $targetDir..." } $folderOutputFixed.GetEnumerator() | sort-Object Name | format-table -autosize | out-file -width $outputWidth $findFilesFoldersOutput -append Write-Host " " Write-Host "Attempting to open scan results with notepad..." c:\windows\system32\notepad.exe "$findFilesFoldersOutput" Write-Host " " Write-Host "Scan saved to: $findFilesFoldersOutput..." Write-Host " " } ################################### # Custom Folder Scan ################################### Function customScan { Write-Host " " write-host "Would you like to scan a specific directory for subfolder sizes?" write-host "Ex C:\, C:\Windows" write-host " " write-host "Press CTRL + C to exit at any time." write-host " " $customLocation= Read-Host 'Please enter directory path here' $subDirectories = Get-ChildItem $customLocation | Where-Object{($_.PSIsContainer)} | foreach-object{$_.Name} Write-Host " " Write-Host "Calculating folder sizes for $customLocation," Write-Host "this process will take a few minutes..." " " | out-file -width $outputWidth $findFilesFoldersOutput -append "Estimated folder sizes for $customLocation :" | out-file -width $outputWidth $findFilesFoldersOutput -append Write-Host " " " " | out-file -width $outputWidth $findFilesFoldersOutput -append $folderOutput = @{} foreach ($i in $subDirectories) { $targetDir = $customLocation + "\" + $i $folderSize = (Get-ChildItem $targetDir -Recurse -Force | Measure-Object -Property Length -Sum).Sum 2> $null $folderSizeComplete = "{0:N0}" -f ($folderSize / 1MB) + "MB" $folderOutput.Add("$targetDir" , "$folderSizeComplete") write-host " Calculating $targetDir..." } $folderOutput.GetEnumerator() | sort-Object Name | format-table -autosize | out-file -width $outputWidth $findFilesFoldersOutput -append Write-Host " " Write-Host "Attempting to open scan results with notepad..." c:\windows\system32\notepad.exe "$findFilesFoldersOutput" Write-Host " " Write-Host "Scan saved to: $findFilesFoldersOutput..." Write-Host " " customScan } # Pause Function Pause { Write-Host -NoNewLine "Press any key to continue..." $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown") } # Clear the screen clear # Clear the output file if it exists by writing a timestamp $time | out-file -width $outputWidth $findFilesFoldersOutput # Execute the functions # Comment out any of the below functions to prevent them from running driveScan fileScan folderScan customScan
Example output:
16-Jun-2016-01-01 Total capacity of C: - 80 GB Total space free on C: - 36 GB / 36,536 MB Below are the 4 largest files on C:\ from largest to smallest: Name : CentOS-7-x86_64-DVD-1511.iso Size In MB : 4,129 Path : C:\Users\Administrator\Downloads LastWriteTime : 5/2/2016 3:03:48 PM Name : smartos-latest.vmwarevm.tar Size In MB : 1,910 Path : C:\Users\Administrator\Downloads\smartos-latest.vmwarevm.tar LastWriteTime : 3/28/2016 4:31:14 PM Name : Fedora-Live-Workstation-x86_64-23-10.iso Size In MB : 1,401 Path : C:\Users\Administrator\Downloads LastWriteTime : 3/24/2016 5:28:00 PM Name : VeeamBackup&Replication_9.0.0.902.iso Size In MB : 1,179 Path : C:\Users\Administrator\Downloads LastWriteTime : 1/18/2016 1:22:35 PM Estimated subfolder sizes for C:\ : Name Value ---- ----- C:\Backup 8MB C:\bginfo 1MB C:\MinecraftServer 34MB C:\PerfLogs 0MB C:\Program Files 3,110MB C:\Program Files (x86) 3,098MB C:\Users 22,475MB C:\VBRCatalog 0MB C:\Windows 17,589MB Estimated folder sizes for c:\windows : Name Value ---- ----- c:\windows\ADFS 1MB c:\windows\AppCompat 6MB c:\windows\apppatch 11MB c:\windows\AppReadiness 0MB c:\windows\assembly 1,793MB c:\windows\Boot 29MB c:\windows\Downloaded Program Files 0MB c:\windows\drivers 0MB c:\windows\en-US 0MB c:\windows\Fonts 504MB c:\windows\Speech 32MB c:\windows\System 0MB c:\windows\System32 3,096MB c:\windows\SystemResources 6MB c:\windows\SysWOW64 1,119MB c:\windows\TAPI 0MB c:\windows\Tasks 0MB c:\windows\Temp 20MB c:\windows\ToastData 0MB c:\windows\tracing 0MB c:\windows\twain_32 0MB c:\windows\Vss 0MB c:\windows\Web 3MB c:\windows\WinStore 2MB c:\windows\WinSxS 7,907MB