Unlocking Folder Size Secrets Using Windows' PowerShell Scripts
Unlocking Folder Size Secrets Using Windows’ PowerShell Scripts
If you want to investigate which folders are taking up your storage space, you can check the folder’s size to determine what’s hogging your drive. Checking folder size is also helpful if you need to move a large folder to a USB drive or cloud storage. An easy way to do this on Windows is to use File Explorer and open the folder Properties dialog.
File Explorer, however, can be slow to determine the size of a large folder. And not ideal if you want to manage files and folders on multiple computers in an organization. To remedy this, you can use PowerShell to quickly calculate folder sizes on Windows.
Disclaimer: This post includes affiliate links
If you click on a link and make a purchase, I may receive a commission at no extra cost to you.
How to Calculate a Folder’s Size Using PowerShell on Windows
To calculate a folder’s size, you’ll need to use the two PowerShell cmdlets, Get-ChildItem and Measure-Object, followed by the Length property and Sum parameter.
The cmdlet Get-ChildItem lets you retrieve information from a specified directory and its sub-directories. The Measure-Object cmdlet and the associated properties and parameters calculate the sum of the length property for the items returned by the Get-ChildItem (alias ‘cgi’) cmdlet.
If you are new to PowerShell, you may want to read our explainer on essential PowerShell cmdlets to understand the basics of PowerShell.
Now that you are familiar with the PowerShell commands, here is how to use them to get any folder size.
- Press the Win key and type powershell.
- Next, right-click on Windows PowerShell and select Run as administrator. Click Yes if prompted by User Account Control.
- In the PowerShell window, type the following command:
Get-ChildItem FolderPath | Measure-Object -Property Length -sum
- In the above command, replace FolderPath with the directory path where your folder is saved. For example, if you want to calculate the size of the Download folder located in the E:\ drive, then the full command will look like this:
Get-ChildItem E:\Download | Measure-Object -Property Length -sum
- The return will show the item count in the folder and its size in bytes. You’ll need to divide the total sum by 1024 to get the size in KBs (Kilobytes). Divide it by 1024 again to get the size in MBs (Megabytes) and so on.
Alternatively, you can use the .sum property to retrieve the total size and divide it by 1 million or billion to convert it into megabytes or gigabytes.
For example, if you want to know the value in gigabytes (MBs), type the following command and press Enter:
(gci E:\Download | measure Length -s).sum / 1Mb
Similarly, replace 1Mb with 1Gb to retrieve the folder size in gigabytes.
(gci E:\Download | measure Length -s).sum / 1Gb
If you want to identify the size of specific types of files in a directory, you can use the wildcard character * followed by the file extension type. It will only show the file size for the specified file type.
For example, to find how much space is taken by the images in a folder, use the following command:
(gci E:\download *.jpg | measure Length -s).sum / 1Mb
Adding a wildcard character lets you determine if a specific file type takes the most space in the folder. You can then filter the contents based on the file extension and delete or move them if necessary.
How to Get the Subfolder Size Using PowerShell
The above command will only calculate the size of your primary directory or folder. To calculate the total size of files in the specified directory and its subdirectories, you’ll need to add the -Recurse parameter to the above command. In addition, we’ll use the -force parameter to access hidden and system files.
For example, to get the total size of your C:\Users directory and its subdirectories, including the hidden files, the PowerShell command will look something like this:
((gci -force c:\Users -Recurse -ErrorAction SilentlyContinue| measure Length -s).sum / 1Gb)
The output in this instance shows 54 GB approx. as the total size of the c:\users folder. In the above command, the -ErrorAction SilentlyContinue parameter is used to suppress any error messages that may occur during the process.
How to Get the Subfolder Size in a Table Format Using PowerShell
If you want to know the size of all the subfolders individually, you can run a PowerShell script to get a graphical view of all the subfolders and their sizes. A script is usually helpful if you have a large directory with multiple subfolders and need to work on them frequently.
To run this script, you can use PowerShell ISE. Here’s how to do it:
- Press the Win key and type PowerShell ISE. Click on Windows PowerShell ISE from the search result.
- Next, copy and paste the following command into the PowerShell ISE console. Make sure to change the directory from c:\ to your preferred directory.
$targetfolder = 'C:\' $dataColl = Get-ChildItem -Force $targetfolder -Directory -ErrorAction SilentlyContinue | ForEach-Object { $len = Get-ChildItem -Recurse -Force $_.FullName -File -ErrorAction SilentlyContinue | Measure-Object -Property Length -Sum | Select-Object -ExpandProperty Sum $foldername = $_.FullName $foldersize = '{0:N2} GB' -f ($len / 1Gb) [PSCustomObject]@{ foldername = $foldername foldersizeGb = $foldersize } } $dataColl | Out-GridView -Title "Size of Subdirectories in $targetfolder"
3. Next, click Run Script or press F5 and wait for the script to execute. Depending on the folder size, you’ll see a “Size Of Subdirectories“ dialog listing all the subdirectories with their size.
In addition to this, you can make use of the PowerShell comparison operators to filter results. For example, to get file size for folders created between June 2023 and July 2023, you can use the following command:
(gci -force E:\Download –Recurse -ErrorAction SilentlyContinue | ? {$_.CreationTime -gt '01/23/23' -AND $_.CreationTime -lt '02/23/23'}| measure Length -s).sum / 1Gb
In the above command, “?” is an alias for the Where-Object cmdlet, -gt, -AND, -It are comparison operators, and CreationTime is a condition. The command checks if the CreationTime of files in the subdirectory falls within the specified date range and shows output only if the condition is satisfied. If you get an error, ensure your date and time format in the command matches the system’s format and try again.
Get the Folder and Subfolder Size Using PowerShell
File Explorer on Windows can help you calculate the size of any folder and file. However, if you work with multiple large folders or in an organization with hundreds of computers to manage, using PowerShell can help you get things done faster.
That said, if you prefer a more GUI-based solution, consider using a disk analyzer tool. These tools can help you visualize the system’s file structure and show a report detailing the contents taking up most space on your system.
File Explorer, however, can be slow to determine the size of a large folder. And not ideal if you want to manage files and folders on multiple computers in an organization. To remedy this, you can use PowerShell to quickly calculate folder sizes on Windows.
Also read:
- [Updated] 2024 Approved Parallel Audio Stream Mapping
- [Updated] Mastering Video (AVI) Into Graphic Image (GIF) Filmora's Complete Guide for 2024
- 2024 Approved Snapchat Cameos Catch Up with the Trendiest Trick
- Crafting a Spectaculous Slideshow in Windows 11, No Fuss
- Elevate Your Software Management Skills Using Windows Package Manager
- How to Counteract Windows Startup Disruption From WinscomrssvDLL
- How to Fix Android App Not Installed Error on Xiaomi Redmi Note 12 Pro+ 5G Quickly? | Dr.fone
- In 2024, Top Pick Windows 10’S Cutting-Edge Apps & Games
- Mastering Windows 10/11: App Resetting Guide
- Overcoming Application Failed to Launch Error Xc000003e on Windows 11
- Quickly Crafting a Facebook Collage A Step-by-Step Guide for 2024
- Tackling Frozen Windows Update With Precision
- Top 5 Optimal eBook Reader Apps for macOS: A Comprehensive Guide
- Top 8 Iconic Cartoon Drawing Techniques Every Enthusiast Must Explore
- Transform Your PC with Adaptive WmDesk
- Transforming Date and Time Formats on Your Windows 11 System: A Step-by-Step Guide
- Uniting Consciousness: Effects of Meditation on Thinking & Feeling
- Title: Unlocking Folder Size Secrets Using Windows' PowerShell Scripts
- Author: David
- Created at : 2024-10-11 19:12:26
- Updated at : 2024-10-15 17:43:38
- Link: https://win11.techidaily.com/unlocking-folder-size-secrets-using-windows-powershell-scripts/
- License: This work is licensed under CC BY-NC-SA 4.0.