Pages

Saturday, February 21, 2015

Function to Get Folder Sizes in Powershell

Following Powershell function can be used to get the sizes of given folders.
Folder names hould be given within double quotes seperated by commas.
 
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
 
 function Get-FolderSize {
    param(
        $FolderPaths = $null
        )
    [array]$FolderLists = $null
    foreach ( $FolderPath in $FolderPaths)
    {
        $FolderLists += 
        Get-ChildItem $FolderPath -Force -Recurse | 
        Measure-Object -property Length -Sum |
        select @{n="Path";e={"""" +$FolderPath + """"}} ,
            @{n="Size GB";e={[math]::ROUND($_.Sum / 1GB -as [Float],2)}} , 
            @{n="Size MB";e={[math]::ROUND($_.Sum / 1MB -as [Float],2)}} , 
            @{n="Size KB";e={[math]::ROUND($_.Sum / 1KB -as [Float],2)}}  
    }
    $FolderLists | Format-Table -AutoSize 
 }

The function rutuns the sizes of given folders in kilobyte (KB), megabyte (MB), gigabyte(GB).

No comments:

Post a Comment