How to Find The Largest Folders Using PowerShell?

One of my team members recently asked me for a script to find the largest folders using PowerShell. I thought I would explain step by step. Since we wanted to manage disk space, it is important to find files and folders that accumulate and consume valuable disk space. In this tutorial, I will explain how to find the largest folder using PowerShell.

You should be familiar with a few basic PowerShell cmdlets as a PowerShell developer.

  • Get-ChildItem: Lists the files and directories in a specified location.
  • Measure-Object: Calculates the properties of objects, such as their size.
  • Sort-Object: Sorts objects based on specified properties.
  • Select-Object: Selects specific properties of objects.

Now, let me show you how to find the largest folders using PowerShell.

Find the Largest Folders Using PowerShell

To find the largest folders, we will use a combination of the Get-ChildItem, Measure-Object, Sort-Object, and Select-Object cmdlets. Let’s go through the process step-by-step.

Step 1: List All Folders

First, we need to list all the folders in a specified directory. Use the Get-ChildItem cmdlet to achieve this. For example, to list all folders in the C:\Bijay directory, run the following command:

Get-ChildItem -Path C:\Bijay -Directory

This command lists all the directories in the C:\Bijay folder. The -Directory parameter ensures that only directories are listed.

Step 2: Calculate Folder Sizes

Next, we need to calculate the size of each folder. We can use the Measure-Object cmdlet to sum the sizes of all files within each folder. Here’s a script that calculates the size of each folder in the C:\Bijay directory:

$folders = Get-ChildItem -Path C:\Bijay -Directory

$folderSizes = foreach ($folder in $folders) {
    $size = (Get-ChildItem -Path $folder.FullName -Recurse -File | Measure-Object -Property Length -Sum).Sum
    [PSCustomObject]@{
        Name = $folder.FullName
        Size = [math]::round($size / 1MB, 2)
    }
}

$folderSizes

This script does the following:

  1. Lists all directories in the specified path.
  2. Iterates through each directory and calculates the total size of all files within it.
  3. Creates a custom object with the folder name and size in megabytes (MB).

Step 3: Sort and Display the Largest Folders

To find the largest folders, we need to sort the results by size and select the top entries. Use the Sort-Object and Select-Object cmdlets to achieve this:

$largestFolders = $folderSizes | Sort-Object -Property Size -Descending | Select-Object -First 10

$largestFolders

This script sorts the folders by size in descending order and selects the top 10 largest folders.

Complete Script

Here’s the complete PowerShell script to find the 10 largest folders in the C:\Bijay directory:

$folders = Get-ChildItem -Path C:\Bijay -Directory

$folderSizes = foreach ($folder in $folders) {
    $size = (Get-ChildItem -Path $folder.FullName -Recurse -File | Measure-Object -Property Length -Sum).Sum
    [PSCustomObject]@{
        Name = $folder.FullName
        Size = [math]::round($size / 1MB, 2)
    }
}

$largestFolders = $folderSizes | Sort-Object -Property Size -Descending | Select-Object -First 10

$largestFolders

I executed the above PowerShell script, and you can see the exact output in the screenshot below:

Find the Largest Folders Using PowerShell

Customize the PowerShell Script

You can customize the script to meet specific requirements. For example, to find the largest folders in a different directory, change the -Path parameter:

$folders = Get-ChildItem -Path C:\Data -Directory

To display the results in gigabytes (GB) instead of megabytes (MB), modify the size calculation:

Size = [math]::round($size / 1GB, 2)

Conclusion

In this tutorial, I explained how to find the largest folders using PowerShell, which can help you manage disk space more effectively. I hope this helps.

You may also like:

100 PowerShell cmdlets download free

100 POWERSHELL CMDLETS E-BOOK

FREE Download an eBook that contains 100 PowerShell cmdlets with complete script and examples.