Home » How to Find Large Files with Get-ChildItem in PowerShell

How to Find Large Files with Get-ChildItem in PowerShell

To find large files in PowerShell, you can use the Get-ChildItem cmdlet to retrieve all files from the specified directory and check for file size to list them by size.

The following methods show how you can find large files in the directory.

Method 1: Find large files greater than 25MB

 $largeFiles = Get-ChildItem  -File | Where-Object {$_.Length -gt 25MB}

This example will return the files having a size greater than 25MB.

Method 2: Find large files and list them by size

Get-ChildItem -File -Recurse | Sort -Descending -Property Length | Select -First 10 Name, Length

The following examples show how you can use these methods.

How to Find Large Files Greater than Specified Size in PowerShell

You can find large files greater than the specified size in PowerShell by using the Get-ChildItem cmdlet and filtering the large files using the Where-Object cmdlet.

# specify the directory path
$folderPath = "C:\temp\log"
# Get large files greater than 25 MB
$largeFiles = Get-ChildItem -Path $folderPath -File | Where-Object {$_.Length -gt 25MB}

Write-Outupt $largeFiles

Output:

PowerShell find large files greater than 25mb
PowerShell find large files greater than 25mb

In this script, we define a $folderPath variable that contains the path to the directory from where you want to retrieve large files greater than 25 MB.

We then use the Get-ChildItem cmdlet to retrieve all files and pipe them to the Where-Object cmdlet to check if the file size is greater than 25MB and store them in the $largeFiles variable.

Finally, we use the Write-Output cmdlet to output the large files in the specified directory.

In the screenshot, we can see that event_logs.txt and system_events.txt files have sizes greater than 25 MB.

How to Get Large Files and List them by Size in PowerShell

To get files and list them by size in PowerShell, you can use the Get-ChildItem cmdlet sort them by Length, and display them in descending order.

# specify the directory path
$folderPath = "C:\temp\log"

# List files ordered by size
Get-ChildItem -Path $folderPath -File -Recurse | Sort -Descending -Property Length | Select -First 10 Name, Length

Output:

PowerShell list large files ordered by size in descending order
PowerShell list large files ordered by size in descending order

In this script, the Get-ChildItem cmdlet retrieves all files from the specified $folderPath and pipes them to the Sort cmdlet to sort them by Length property in descending order.

Finally, the Select cmdlet selects the top 10 large files’ Names and lengths.

Conclusion

I hope the above article on finding large files in PowerShell using the Get-ChildItem cmdlet is helpful to you.

You can find more topics about Active Directory tools and PowerShell basics on the ActiveDirectoryTools home page.