0
0
PowershellHow-ToBeginner · 2 min read

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, Length
Output
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.

FileSize (bytes)Is > 100MB?
C:\file1.txt50000No
C:\Windows\System32\largefile.sys150000000Yes
C:\Users\User\Videos\movie.mp4200000000Yes
💡

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, Length
This 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 -Sum
This 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.

ApproachTimeSpaceBest For
Filter by sizeO(n)O(m)Finding all files above size
Sort and select topO(n log n)O(n)Finding top largest files
Measure total sizeO(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.