PowerShell Script to Find Large Files Quickly
Use
Get-ChildItem -Path -Recurse -File | Where-Object { $_.Length -gt } to find files larger than a specified size in PowerShell.Examples
InputGet-ChildItem -Path C:\ -Recurse -File | Where-Object { $_.Length -gt 100MB }
OutputLists all files larger than 100MB in C:\ and its subfolders.
InputGet-ChildItem -Path D:\Documents -Recurse -File | Where-Object { $_.Length -gt 500000000 }
OutputShows files bigger than 500,000,000 bytes (about 500MB) in D:\Documents and subfolders.
InputGet-ChildItem -Path C:\Temp -Recurse -File | Where-Object { $_.Length -gt 0 }
OutputLists all files with size greater than 0 bytes in C:\Temp and its subfolders.
How to Think About It
To find large files, first look through all files in a folder and its subfolders. Then check each file's size using its length property. Finally, keep only those files whose size is bigger than the size you want.
Algorithm
1
Get the folder path and size limit from the user or set defaults.2
List all files in the folder and all subfolders.3
For each file, check if its size is greater than the size limit.4
Collect and display the files that meet the size condition.Code
powershell
Get-ChildItem -Path C:\ -Recurse -File | Where-Object { $_.Length -gt 100MB } | Select-Object FullName, LengthOutput
FullName Length
-------- ------
C:\Windows\System32\largefile.sys 150000000
C:\Users\User\Videos\movie.mp4 200000000
Dry Run
Let's trace finding files larger than 100MB in C:\ folder.
1
List files
Get all files in C:\ and subfolders.
2
Check size
For each file, check if size > 100MB (104857600 bytes).
3
Output files
Show files that passed the size check.
| File | Size (bytes) | Is > 100MB? |
|---|---|---|
| C:\file1.txt | 50000 | No |
| C:\Windows\System32\largefile.sys | 150000000 | Yes |
| C:\Users\User\Videos\movie.mp4 | 200000000 | Yes |
Why This Works
Step 1: Get-ChildItem lists files
The Get-ChildItem command lists all files in the folder and subfolders using -Recurse.
Step 2: Filter by size
The Where-Object filters files where Length (size in bytes) is greater than the given size.
Step 3: Display results
The script shows the full path and size of each large file found.
Alternative Approaches
Sort and select top largest files
powershell
Get-ChildItem -Path C:\ -Recurse -File | Sort-Object Length -Descending | Select-Object -First 10 FullName, LengthThis finds the 10 largest files instead of filtering by size.
Using Measure-Object for total size
powershell
Get-ChildItem -Path C:\ -Recurse -File | Where-Object { $_.Length -gt 100MB } | Measure-Object -Property Length -SumThis calculates the total size of all large files found.
Complexity: O(n) time, O(m) space
Time Complexity
The script checks each file once, so time grows linearly with the number of files (n).
Space Complexity
It stores only files larger than the threshold (m), which is usually less than total files.
Which Approach is Fastest?
Filtering with Where-Object is efficient; sorting all files is slower for large sets.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Filter by size | O(n) | O(m) | Finding all files above size |
| Sort and select top | O(n log n) | O(n) | Finding top largest files |
| Measure total size | O(n) | O(m) | Summing sizes of large files |
Use
-File with Get-ChildItem to avoid listing folders.Forgetting to use
-Recurse causes only the top folder to be searched.