Dissecting Directory Size: A PowerShell Approach
Dissecting Directory Size: A PowerShell Approach
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.
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.
TubeDigger - online video downloader from mostly any site
How to Get the Subfolder Size Using PowerShell
- Title: Dissecting Directory Size: A PowerShell Approach
- Author: Richard
- Created at : 2024-08-16 01:45:24
- Updated at : 2024-08-17 01:45:24
- Link: https://win11-tips.techidaily.com/dissecting-directory-size-a-powershell-approach/
- License: This work is licensed under CC BY-NC-SA 4.0.