Skip to main content

PowerShell script to retrieve information about disk usage in the current directory and all subdirectories. If you specify the "-IncludeSubdirectories" flag, this script accounts for the size of subdirectories in the size of a directory. Also see Measure the Files in a Directory with PowerShell .

##############################################################################
##
## Get-DiskUsage
##
## From Windows PowerShell Cookbook (O'Reilly)
## by Lee Holmes (http://www.leeholmes.com/guide)
##
##############################################################################

<#

.SYNOPSIS

    Retrieve information about disk usage in the current directory and all
    subdirectories. If you specify the -IncludeSubdirectories flag, this script
    accounts for the size of subdirectories in the size of a directory.

.EXAMPLE

    Gets the disk usage for the current directory:

        PS > Get-DiskUsage

.EXAMPLE

    Gets the disk usage for the current directory and those below it,
    adding the size of child directories to the directory that contains them:

    PS > Get-DiskUsage -IncludeSubdirectories

#>

param(
    ## Switch to include subdirectories in the size of each directory
    [switch] $IncludeSubdirectories
)

Set-StrictMode -Version 3

## If they specify the -IncludeSubdirectories flag, then we want to account
## for all subdirectories in the size of each directory
if($includeSubdirectories)
{
    Get-ChildItem -Directory |
        Select-Object Name,
            @{ Name="Size";
            Expression={ ($_ | Get-ChildItem -Recurse |
                Measure-Object -Sum Length).Sum + 0 } }
}
## Otherwise, we just find all directories below the current directory,
## and determine their size
else
{
    Get-ChildItem -Recurse -Directory |
        Select-Object FullName,
            @{ Name="Size";
            Expression={ ($_ | Get-ChildItem |
                Measure-Object -Sum Length).Sum + 0 } }
}