How to Calculate Folder Size Using PowerShell on Windows

How to Calculate Folder Size Using PowerShell on Windows

Introduction: How to Calculate Folder Size

Uncover the mystery of your storage space! Discover how to identify the folders that are gobbling up your precious disk space on Windows. This guide will walk you through the process using PowerShell to efficiently Calculate Folder sizes.

Using PowerShell for Efficient Folder Size Calculation

Calculate Folder
Image by https://www.makeuseof.com/

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.

  1. Press the Win key and type powershell.
  2. Next, right-click on Windows PowerShell and select Run as administrator. Click Yes if prompted by User Account Control.
  3. In the PowerShell window, type the following command:
     Get-ChildItem FolderPath | Measure-Object -Property Length -sum
  4. 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
  5. 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.

Calculate Folder
Image by https://www.makeuseof.com/

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:

Calculate Folder
Image by https://www.makeuseof.com/

Step-by-Step Guide to Calculating Folder Size with PowerShell

Are you new to PowerShell? No worries! We’ve got you covered with a step-by-step guide to calculating folder sizes effortlessly. Follow these simple instructions to become a master at evaluating storage consumption:

  1. Launch PowerShell as an administrator.
  2. Execute the command:
  3. Replace “FolderPath” with your directory path.
  4. Calculate sizes in different units: KBs, MBs, GBs.
  5. Opt for simplicity:

Advanced Techniques: Subfolder Size and Graphical View

Calculate Folder
Image by https://www.makeuseof.com/

Looking to dig deeper? PowerShell offers advanced techniques to measure subfolder sizes and gain a graphical perspective. Learn how to:

  1. Get Subfolder Size: Explore the command to calculate total sizes including subdirectories and hidden files.
  2. Graphical View with PowerShell ISE: Use a script for a comprehensive graphical view of subfolder sizes.
    • Copy and paste the script into PowerShell ISE.
    • Run the script to visualize subfolder sizes.
  3. Filtering with PowerShell Comparison Operators: Uncover how to filter by specific criteria, like creation dates, using PowerShell comparison operators.

    How to Get the Subfolder Size in a Table Format Using PowerShell

    Calculate Folder
    Image by https://www.makeuseof.com/

    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:

    1. Press the Win key and type PowerShell ISE. Click on Windows PowerShell ISE from the search result.
      Calculate Folder
      Image by https://www.makeuseof.com/

      Next, copy and paste the following command into the PowerShell ISE console. Make sure to change the directory from c:\ to your preferred directory.

      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.

Conclusion: Power Up Your Storage Management

PowerShell transforms the way you manage storage space. Whether you’re dealing with a single folder or exploring the depths of a complex directory structure, PowerShell equips you with the tools to streamline and optimize your storage management tasks. However, if a GUI-based approach is more your style, consider utilizing disk analyzer tools for a visual representation of your system’s storage landscape.

 

author

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *