PowerShell: How to Find N Largest Files in a Directory


You can use the following syntax in PowerShell to find the N largest files in a directory:

Get-ChildItem -File -Recurse |
    Sort Length -descending |
    Select FullName, Length -first 10

This particular example will return the 10 largest files in the current directory.

Here is what the various commands do:

  • Get-ChildItem -File -Recurse: Retrieve all files in current directory, recursively
  • Sort Length -descending: Sort files by size from largest to smallest
  • Select FullName, Length: Only return the full file path and file size
  • -first 10: Only display 10 largest files

Feel free to replace 10 with a different number to instead return the N largest files.

The following example shows how to use this syntax in practice.

Example: Use PowerShell to Find N Largest Files in a Directory

We can use the Get-ChildItem cmdlet with the following syntax to view all files in our current directory:

Get-ChildItem -File -Recurse | Select FullName

The following screenshot shows how to use this syntax in practice:

Now suppose that we would only like to return the 10 largest files in the current directory, sorted from largest to smallest.

We can use the following syntax to do so:

Get-ChildItem -File -Recurse |
    Sort Length -descending |
    Select FullName, Length -first 10

The following screenshot shows how to use this syntax in practice:

PowerShell find N largest files in directory

This returns the 10 largest files in the current directory, sorted by size from largest to smallest.

Feel free to replace 10 with a different number to instead return a different number of the largest files.

Related Tutorials

The following tutorials explain how to perform other common tasks in PowerShell:

PowerShell: How to Sort Files in Folder by Size
PowerShell: How to Get Folder Size in GB
PowerShell: How to List Files in Directory by Date
PowerShell: How to Get Most Recently Modified File in Directory

Leave a Reply