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 -DirectoryThis 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)
}
}
$folderSizesThis script does the following:
- Lists all directories in the specified path.
- Iterates through each directory and calculates the total size of all files within it.
- 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
$largestFoldersThis 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
$largestFoldersI executed the above PowerShell script, and you can see the exact output in the screenshot below:

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 -DirectoryTo 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:
- PowerShell Copy-item Create Folder If Not Exist
- How to Check If a Folder Exists in PowerShell?
- How to Check if a Folder is Empty in PowerShell?
- How to Create folder if not exist in PowerShell?
- How to Delete Contents of Folder in PowerShell?
Bijay Kumar is an esteemed author and the mind behind PowerShellFAQs.com, where he shares his extensive knowledge and expertise in PowerShell, with a particular focus on SharePoint projects. Recognized for his contributions to the tech community, Bijay has been honored with the prestigious Microsoft MVP award. With over 15 years of experience in the software industry, he has a rich professional background, having worked with industry giants such as HP and TCS. His insights and guidance have made him a respected figure in the world of software development and administration. Read more.