Determine directory size with PowerShell

[January 18, 2015] Update: I’ve made a couple of updates, including an if statement to determine the best unit of measure.

You can use PowerShell to determine the size of a directory. The benefit of this is that you do not need to install an additional utility (like the du utility, previously mentioned) and that the information can easily be used in scripts. There are some drawbacks, however.

In order to get the size of a directory, we have to add up the size of all of the items within the directory and within its sub-directories.

PS C:\> ( Get-ChildItem C:\myfolder\ -Recurse -Force | Measure-Object -Property Length -Sum ).Sum
17723191811

This will return the summed result, in bytes, of the size (length property) of all of the items below C:\myfolder\.

We can also convert this to kilobytes, megabytes, gigabytes, terabytes – do any of you have a petabyte worth of data? – well, we can do that too:

PS C:\> ( Get-ChildItem C:\myfolder\ -Recurse -Force | Measure-Object -Property Length -Sum ).Sum / 1GB
16.5060086278245

Replace “1GB” with “1KB” (kilobytes), “1MB” (megabytes), “1TB” (terabytes), or “1PB” (petabytes).

Let’s dress up the output a little bit, by formatting it as a number and cutting it down to two decimal places:

PS C:\> "{0:N2}" -f ( ( Get-ChildItem C:\myfolder\ -Recurse -Force | Measure-Object -Property Length -Sum ).Sum / 1GB )
16.51

See the references below for more information on formatting numbers.

Suggested in the comments, this if statement will determine the best unit of measure and set the output appropriately:

$folder = "C:\myfolder\"
$folderSize = ( Get-ChildItem $folder -Recurse -Force | Measure-Object -Property Length -Sum ).Sum
If ( $folderSize -lt 1KB ) { $folderSizeOutput = "$("{0:N2}" -f $folderSize) B" }
ElseIf ( $folderSize -lt 1MB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1KB)) KB" }
ElseIf ( $folderSize -lt 1GB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1MB)) MB" }
ElseIf ( $folderSize -lt 1TB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1GB)) GB" }
ElseIf ( $folderSize -lt 1PB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1TB)) TB" }
ElseIf ( $folderSize -ge 1PB ) { $folderSizeOutput = "$("{0:N2}" -f ($folderSize / 1PB)) PB" }
Write-Output $folderSizeOutput

This method isn’t perfect. For instance, if you don’t have access to a file or folder under the path you are measuring, then PowerShell will not be able to get the file information. Luckily, it will throw an error, but continue adding up the other files that it can access. Unfortunately, this may give a false representation of size, depending what it is that could not be accessed. At least the error message can shed some light on the problem.

References

2 Comments


  1. make an if statement so that it automatically chooses between KB, MB or GB output based on the total size. also a good cmdlet would accept input other than using read-host.
    but that involves real programming with c#

    Reply

    1. Thanks for the comment. Excellent suggestion on the If statement – I’ve added an example to the post.

      A bit off topic, I agree about accepting input as part of a cmdlet instead of using read-host. Of course, it depends on the circumstances. The usage that lead me to originally write the post was checking a specific folder and didn’t warrant getting user input – as reflected in my examples.

      Reply

Leave a Reply