PowerShell: Dir Size

By Xah Lee. Date: . Last updated: .

Directory Size

put this in your PowerShell Profile

function xah-dir-size {

    # .DESCRIPTION
    # Show dir size (counts all subdirs)
    # First arg is path of a dir. Default to current dir.
    # .NOTES
    # Version: 2022-06-07 2022-06-28 2022-08-28
    # .LINK
    # http://xahlee.info/powershell/powershell_dir_size.html

    Param( [string] $path )
    $total = ((Get-ChildItem $path -File -Recurse | Measure-Object -Property Length -Sum).sum);
    "{0:n0} bytes" -f $total;
}

Show Sizes of Subdirs

function xah-subdir-sizes {

    # .DESCRIPTION
    # Show every subdir size.
    # First arg is path of a dir. Default to current dir.
    # .NOTES
    # Version: 2022-06-07 2024-03-08
    # .LINK
    # http://xahlee.info/powershell/powershell_dir_size.html

    Param( [string] $path )
    Get-ChildItem $path -Directory | foreach {
        $total = ((Get-ChildItem $_ -File -Recurse | Measure-Object -Property Length -Sum).sum );
        Write-Host $_.name " " -NoNewline; "{0:n0} bytes" -f $total; }
}

PowerShell, Working with Directory