Get size of all items in current path

I was looking for an easy way to get the size of all user profiles folder through PowerShell and found this woshub and inspired by it ended with the following function which measures the size of everything in current path if used without parameter. Made it a function just to be able to autoload it. It’s nothing special, however, it’s surprisingly useful 🙂

function Get-Size {
    param (
        [Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Path="$pwd"
    )
    Get-ChildItem -force $Path -ErrorAction SilentlyContinue | ForEach-Object {
        $len = 0
        Get-ChildItem -recurse -force $_.fullname -ErrorAction SilentlyContinue | ForEach-Object { $len += $_.length }
        $_.fullname, '-- {0:N2} GB' -f ($len / 1Gb)
        $sum = $sum + $len
        }
        "Total size of everything",'-- {0:N2} GB' -f ($sum / 1Gb)   
}

If you want folders only it will be:

function Get-SizeFO {
    param (
        [Parameter(Position=0,ValueFromPipeline=$true,ValueFromPipelineByPropertyName=$true)]
        [String[]]$Path="$pwd"
    )
    Get-ChildItem -force $Path -ErrorAction SilentlyContinue |  Where-Object { $_ -is [io.directoryinfo] }  | ForEach-Object {
        $len = 0
        Get-ChildItem -recurse -force $_.fullname -ErrorAction SilentlyContinue | ForEach-Object { $len += $_.length }
        $_.fullname, '-- {0:N2} GB' -f ($len / 1Gb)
        $sum = $sum + $len
        }
        "Total size of everything",'-- {0:N2} GB' -f ($sum / 1Gb)   
}

The only difference is Where-Object { $_ -is [io.directoryinfo] }. To switch to Megabytes replace Gb with Mb. Works with PowerShell 3/4/5/6/7.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.