PowerShell: How to Get the Folder Size?
As a system administrator or IT professional, it’s essential to be able to efficiently measure the size of directories or folders on your computer or server, allocate disk space, and identify storage-intensive directories effectively. While File Explorer provides a basic overview of folder sizes, PowerShell offers more advanced capabilities for precise and detailed folder size analysis and automation scenarios. In this article, we’ll explore various PowerShell commands and tips that will allow you to get folder sizes with ease.
Table of contents
- Introduction to PowerShell and its capabilities for getting folder sizes
- Basic PowerShell command for getting folder sizes
- Get folder size including subfolders and Export to CSV using PowerShell
- PowerShell for getting folder size in Megabytes or Gigabytes
- PowerShell for listing folder sizes
- Getting the size of a folder excluding certain File Types
- Common errors and troubleshooting tips
- Comparing PowerShell Methods and Alternative Tools
Introduction to PowerShell and its capabilities for getting folder sizes
PowerShell is a command-line shell and scripting language that’s built on the .NET framework. With PowerShell, you can automate administrative tasks, manage system configurations, and access system components and data. PowerShell provides a powerful set of commands that can help you get folder sizes within seconds, freeing up time and making work more efficient. In this comprehensive guide, we will explore various PowerShell techniques to get folder sizes accurately. We will delve into the usage of core cmdlets like Get-ChildItem and Measure-Object to retrieve and calculate folder sizes.
You can use PowerShell to get the size of a folder, including subfolders, in GB or MB, and to list the size of directories or folders.
Basic PowerShell command for getting folder sizes
The simplest way to get the size of a folder in PowerShell is to use the Get-ChildItem
cmdlet with the -Path
parameter and pipe the output of the Get-ChildItem cmdlet to the Measure-Object cmdlet, specifying the “Length” property. Here’s an example:
Get-ChildItem -Path "C:\Temp" | Measure-Object -Property Length -Sum
This command will return the size of the folder C:\Temp
and all its files in bytes. The Measure-Object
cmdlet with the -Property
parameter and the -Sum
switch is used to calculate the total size of the folder and all its subfolders. You can also use the alias GCI for Get-ChildItem cmdlet.
Get folder size including subfolders and Export to CSV using PowerShell
If you want to include the sizes of subfolders in the folder size calculation, you can utilize the -Recurse parameter with the Get-ChildItem cmdlet. This parameter enables recursive search, allowing PowerShell to retrieve the contents of subfolders as well. Here is an example:
(Get-ChildItem -Path C:\Temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB
This command will return the size of the folder C:\Temp
and all its subfolders in MB. If you want to get the size in GB, simply change / 1MB
to / 1GB
. How about exporting all Folders and Sub-Folder sizes to a CSV file for better analysis?
#Parameters
$path = "C:\Temp"
$outputFile ="C:\Temp\FolderSize.csv"
#Get all Folders, Sub-Folders recursively
$folders = Get-ChildItem -Path $Path -Directory -Recurse
#Loop through each folder to Find the size
$FolderSizes = foreach ($folder in $folders) {
$size = (Get-ChildItem -Path $folder.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
$sizeInMB = $size / 1MB
#Collect Data
[PSCustomObject]@{
FolderName = $folder.FullName
SizeInMB = [Math]::Round($sizeInMB,2)
}
}
#Export the Result to CSV
$FolderSizes | Format-table
$FolderSizes | Export-Csv -Path $outputFile -NoTypeInformation
Adjusting the Size Calculation for Subfolders
When including subdirectories in the size calculation, it’s important to note that the result will be the combined size of all files and folders. However, if you want to obtain the size of each subfolder individually along with the total size, you can modify the command slightly.
Get-ChildItem -Recurse -Path C:\Temp | ForEach-Object {
$size = (Get-ChildItem -File -Path $_.FullName | Measure-Object -Property Length -Sum).Sum
[PSCustomObject]@{
Folder = $_.FullName
Size = $size
}
}
Replace C:\Temp
with the actual path of the folder, you want to analyze. This command will display the size of each subfolder along with its full path.
PowerShell for getting folder size in Megabytes or Gigabytes
By default, the size calculated using the Measure-Object cmdlet is displayed in bytes. If you want to convert the size into more human-readable formats like megabytes (MB) or gigabytes (GB), we can divide the sum by the appropriate conversion factor. To get the size of a folder in GB, you can use the following command:
To display the size in megabytes, divide the sum by 1,048,576 (1 MB in bytes):
(Get-ChildItem -File -Path C:\FolderPath | Measure-Object -Property Length -Sum).Sum / 1MB
Similarly, to get the size in GB, divide the output by 1 GB.
[math]::Round((Get-ChildItem -Path C:\Temp -Recurse | Measure-Object -Property Length -Sum).Sum / 1GB,2)
This command will return the size of the folder C:\Temp
and all its subfolders in GB. Also we have used the [math]::Round()
function to get two-decimal places.
PowerShell for listing folder sizes
You can also use PowerShell to list the size of directories or folders on your computer or server. Here’s an example:
Get-ChildItem -Path C:\ -Directory | ForEach-Object { $_.FullName + ": " + ((Get-ChildItem $_.FullName -Recurse | Measure-Object -Property Length -Sum).Sum / 1MB) + " MB" }
This command will list the size of all the directories or folders in the C:\
of your hard drive in MB.
Getting the size of a folder excluding certain File Types
If you want to get the size of a folder and its subfolders, but exclude certain files, you can use the following command:
(Get-ChildItem -Path C:\Logs -Recurse -Exclude *.log | Measure-Object -Property Length -Sum).Sum / 1MB
This command will return the size of the folder C:\Logs
and all its subfolders, excluding all files with the .log
extension.
Filtering Files by Type
If you want to calculate the size of specific file types within a specified directory, you can filter the files using the Get-ChildItem cmdlet before performing the size calculation. For example, to calculate the size of only PDF files in a folder, use the following command:
(Get-ChildItem -File -Path C:\Reports -Filter *.pdf | Measure-Object -Property Length -Sum).Sum
Replace C:\Reports
with the actual path of the folder you want to analyze and *.pdf
with the desired file extension. You can further filter the files based on specific attributes such as “Created Date” with the help of the “Where-Object” cmdlet and calculate the size. E.g.
$Path = "C:\Temp"
$StartDate = (Get-Date).AddDays(-7)
#Get All Files created in the past 7 days
$Files = Get-ChildItem -Path $path -File -Recurse | Where-Object { $_.CreationTime -ge $startDate }
$TotalSize = ($Files | Measure-Object -Property Length -Sum).Sum
$totalSizeInMB = $totalSize / 1MB
Write-Host "Total Size of Files Created in the Past 7 Days: $totalSizeInMB MB"
Common errors and troubleshooting tips
If you encounter errors when using PowerShell to get folder sizes, here are some troubleshooting tips:
- Make sure you have the correct permissions to access the folders you’re trying to measure. Run the PowerShell script as Administrator. Otherwise, You’ll get “Get-ChildItem : Access to the path ‘C:\Temp\Reports” error.
- Check that you’re using the correct path to the folder.
- Make sure you’re using the correct syntax for the command. Review the examples in this article and consult the PowerShell documentation if you’re unsure.
Comparing PowerShell Methods and Alternative Tools
While PowerShell provides powerful capabilities for folder size analysis, it’s essential to consider the pros and cons of using PowerShell methods and explore alternative tools for specific use cases.
Pros and Cons of PowerShell Methods
Pros:
- Flexibility and Customization: PowerShell allows for extensive customization and scripting capabilities, making it suitable for complex folder size analysis scenarios.
- Integration with Other PowerShell Commands: PowerShell cmdlets can be combined with other PowerShell commands to automate tasks and perform advanced analysis.
- Built-in PowerShell Modules: PowerShell has various built-in modules and cmdlets, such as Measure-Object and Get-ChildItem, that provide powerful features for folder size analysis.
Cons:
- Learning Curve: PowerShell has a learning curve, especially for beginners, who may find it challenging to grasp the syntax and nuances of PowerShell scripting.
- Execution Policy Restrictions: PowerShell execution policies may restrict the execution of scripts and commands, requiring additional configuration to run certain commands.
- Complexity for Simple Tasks: For straightforward folder size analysis, using PowerShell cmdlets may involve writing and executing multiple commands, which can be time-consuming.
Exploring Alternative Tools for Folder Size Analysis
While PowerShell is a versatile tool for folder size analysis, alternative tools can provide more user-friendly interfaces and additional functionalities. Here are a few alternative tools worth exploring:
- TreeSize: TreeSize is a popular third-party tool that offers a visual representation of folder sizes and provides additional features like file search and duplicate file detection.
- WinDirStat: WinDirStat is another widely used tool that provides a graphical representation of folder sizes and allows users to identify storage-intensive files and folders easily.
- Folder Size Explorer: Folder Size Explorer is a lightweight tool that provides a detailed view of folder sizes, allowing users to analyze disk space usage efficiently.
When choosing an alternative tool, consider factors such as ease of use, specific features required, and compatibility with your operating system.
Conclusion
In this comprehensive guide, we explored various methods and techniques to get folder sizes using PowerShell. We learned how to use core cmdlets like Get-ChildItem and Measure-Object to calculate folder sizes and display them in human-readable formats accurately. WMI and NET framework also offers ways to get folder size using PowerShell. Nevertheless, a more efficient and faster method is by using the native PowerShell method. Whether you choose to use PowerShell cmdlets or alternative tools, understanding how to retrieve and analyze folder sizes is essential for efficient disk space management.
By following the best practices and troubleshooting tips provided, you can optimize your folder size analysis and gain valuable insights into your file and folder storage. With the knowledge gained from this guide, you are well-equipped to become proficient in measuring and managing folder sizes using PowerShell.
This was incredibly helpful! You can also filter by folder size by just adding a couple lines:
$FolderSizes = foreach ($folder in $folders) {
$size = (Get-ChildItem -Path $folder.FullName -File -Recurse | Measure-Object -Property Length -Sum).Sum
$sizeInMB = $size / 1MB
if ($sizeInMB -gt 1000){
#Collect Data
[PSCustomObject]@{
FolderName = $folder.FullName
SizeInMB = [Math]::Round($sizeInMB,2)
}
}
}