Taming Byte-Size Information Through PowerShell Execution

Taming Byte-Size Information Through PowerShell Execution

Richard Lv13

Taming Byte-Size Information Through PowerShell Execution

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

powershell cmdlet to view folder size bytes

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.

powershell cmdlet to view folder size megabytes 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:

powershell cmdlet to view folder by file type

(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

powershell cmdlet to view folder subfolder size

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

windows powershell ISE script folder size view

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.
    powershell ise start menu
  2. 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:

  • Title: Taming Byte-Size Information Through PowerShell Execution
  • Author: Richard
  • Created at : 2024-11-30 18:32:26
  • Updated at : 2024-12-07 00:52:32
  • Link: https://win11-tips.techidaily.com/taming-byte-size-information-through-powershell-execution/
  • License: This work is licensed under CC BY-NC-SA 4.0.