How to Get the Size of the Folder Including the Subfolders in PowerShell

  1. Using Get-ChildItem and Measure-Object
  2. Using PowerShell Function
  3. Using Windows Explorer for Quick Checks
  4. Conclusion
  5. FAQ
How to Get the Size of the Folder Including the Subfolders in PowerShell

Managing disk space is crucial for any system administrator or user. One common task is determining the size of a folder, including all its subfolders. PowerShell, a powerful scripting language and command-line shell, provides a straightforward way to accomplish this. In this tutorial, we will explore different methods to calculate the folder size using PowerShell commands.

Whether you’re cleaning up your hard drive or preparing for a backup, knowing the total size of a directory can save you time and effort. With PowerShell, you can easily retrieve this information without needing third-party tools. Let’s dive into the methods that will help you get the folder size, including subfolders.

Using Get-ChildItem and Measure-Object

One of the simplest ways to get the size of a folder, including its subfolders, is by using the Get-ChildItem cmdlet combined with Measure-Object. This method allows you to list all files and their sizes within a specified directory and sum them up.

Here’s how you can do it:

$folderPath = "C:\Your\Folder\Path"
$folderSize = (Get-ChildItem -Path $folderPath -Recurse | Measure-Object -Property Length -Sum).Sum
$folderSizeMB = [math]::round($folderSize / 1MB, 2)
Write-Output "Total size of the folder is $folderSizeMB MB"

After running this code, you should see an output that provides the total size of the specified folder in megabytes.

Output:

Total size of the folder is 15.25 MB

In this script, we first set the folder path in the variable $folderPath. The Get-ChildItem cmdlet retrieves all files within the specified folder and its subfolders due to the -Recurse parameter. The output of this command is then piped into Measure-Object, which calculates the total size by summing up the lengths of all files. Finally, we convert the size from bytes to megabytes for easier readability.

Using PowerShell Function

For a more reusable approach, you can create a PowerShell function that calculates the folder size. This function can be called anytime you need to check the size of a folder.

Here’s a sample function:

function Get-FolderSize {
    param (
        [string]$folderPath
    )
    
    if (Test-Path $folderPath) {
        $folderSize = (Get-ChildItem -Path $folderPath -Recurse | Measure-Object -Property Length -Sum).Sum
        $folderSizeMB = [math]::round($folderSize / 1MB, 2)
        return "Total size of the folder is $folderSizeMB MB"
    } else {
        return "Folder path does not exist."
    }
}

Get-FolderSize -folderPath "C:\Your\Folder\Path"

Output:

Total size of the folder is 20.45 MB

In this example, we define a function named Get-FolderSize, which takes one parameter: the folder path. The function checks if the specified path exists using Test-Path. If it does, it calculates the total size using the same Get-ChildItem and Measure-Object combination as before. The result is formatted and returned to the user. This function can be easily reused for different folder paths, making it a versatile tool in your PowerShell arsenal.

Using Windows Explorer for Quick Checks

While PowerShell is a powerful tool for scripting and automation, sometimes the quickest way to check a folder size is through Windows Explorer. This method is especially useful if you need a quick overview without running any scripts.

To check the size of a folder using Windows Explorer, follow these steps:

  1. Navigate to the folder you want to check.
  2. Right-click on the folder and select “Properties.”
  3. In the Properties window, you will see the size of the folder, including all subfolders and files.

This method provides a straightforward visual representation of the folder size, and you can also see additional details such as the number of files and subfolders it contains.

Conclusion

Knowing how to get the size of a folder, including its subfolders, is an invaluable skill for anyone managing files on a computer. PowerShell offers powerful commands that can help automate this task efficiently. Whether you prefer using simple cmdlets, creating reusable functions, or checking folder sizes via Windows Explorer, you now have the tools you need to manage your disk space effectively. By mastering these methods, you can ensure that your system remains organized and free of unnecessary files.

FAQ

  1. How can I get the size of a folder without using PowerShell?
    You can right-click the folder in Windows Explorer and select “Properties” to view its size, including subfolders.

  2. Does PowerShell allow me to get folder sizes for remote directories?
    Yes, you can use PowerShell to access remote directories by specifying the UNC path in the Get-ChildItem command.

  3. Can I get the size of multiple folders at once using PowerShell?
    Yes, you can loop through multiple folder paths in a script to get their sizes using the same methods described above.

  4. Is there a graphical tool available for checking folder sizes?
    Yes, there are several third-party tools, such as WinDirStat and TreeSize, that provide graphical representations of folder sizes.

  5. Can I schedule a PowerShell script to run at specific intervals to monitor folder sizes?
    Yes, you can use Windows Task Scheduler to run PowerShell scripts at specified intervals for monitoring purposes.

Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
Author: Sheeraz Gul
Sheeraz Gul avatar Sheeraz Gul avatar

Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.

LinkedIn Facebook

Related Article - PowerShell Folder