Getting the Directory Size on Remote Servers.

Greetings Kenny

In regards to your question on scanning for a directory size…

I think I would use a different approach for finding the results of a remote directory size.  A good choice for this is the “Get-ChildItem” cmdlette.

The DOS version of this is simply the DIR command.

Unlike VBScript, Powershell doesn’t have a direct method for getting a folder size, but there is a work around. You can use the “Measure-Object –Sum” cmdlette and properties to calculate the data.

Let’s say I want to calculate the size of the c:\temp directory on my local computer.

The command would look like this:

We are creating a resulting PowerShell object with the properties of count, average, sum, etc.

We are only assigning a value to Sum.

So how would that be used to calculate the size of a folder on a remote server?  Instead of using “c:\temp” you could put in any path, including a URL.  Here is the conceptual example.

I am using –recurse to check all subfolders, and –force to check hidden and system files.

Get-ChildItem \\servername\c$\temp -recurse -force | select Length |Measure-Object -Sum length

In your comment you indicated that you wanted to check the size of E:\VM on multiple servers.  You could pull the server name out of a text file.  The text file could be called server.txt

Assuming I put the file containing my list of servers to check at c:\servers.txt, the command could like this.

$path = “\\$serverName\E$\VM

foreach ($serverName in (get-content c:\servers.txt))


       $dirSize = Get-ChildItem $path -recurse -force | select Length  |Measure-Object -Sum length    

       $dirSize #Output the result to the screen.


That works, but the result is kind of awkward as output is in bytes instead of MB.

We can dress this up by applying formatting to the result, and converting to MB.

$dirSize = Get-ChildItem $path -recurse -force | select Length  |Measure-Object -Sum length

$dirSize.sum = $dirSize.sum/1MB

$finalResult = “{0:N2} MB” -f $result.sum

Now the resulting directory size would be in MB.

One more way to improve this would be to collect each iteration of the “foreach” cmdlette, and put into an object so that would could get the result into a CSV file, or maybe output to the out-gridview cmdlette.

Here is the result of all that code..

$dataColl = @()#Makes an array, or a collection to hold all the object of the same fields.

foreach ($serverName in (get-content c:\servers.txt))


       $path = “\\$serverName\e$\VM

       $dirSize = Get-ChildItem $path -recurse -force | select Length  |Measure-Object -Sum                   length

       $dirSize.sum = $dirSize.sum/1MB

       $finalResult = “{0:N2} MB” -f $result.sum

       $dataObject = New-Object PSObject

       Add-Member -inputObject $dataObject -memberType NoteProperty -name “ServerName” -value                 $serverName

       Add-Member -inputObject $dataObject -memberType NoteProperty -name “Dir_Size” -value                   $finalResult

       $dataColl += $dataObject  



$dataColl | Out-GridView -Title “Remote Directory Scan Results”

$dataColl | Export-Csv -noTypeInformation -path c:\temp.csv

Here is the resulting “Out-GridView”.  I’ve smudged out the actual server names I used.

The resulting output is also available in the temp.CSV file at the root of my C: drive.

Depending on your number of servers, network, and folder sizes this could take a while to run.  I would save it to a script and run it from a separate Powershell window so you don’t have to stare at it while it runs.

Please let me know if this helps, and if you have any further questions.

Have a nice day.



About Patrick
I am a Senior Systems Administrator for AT&T. I have been with AT&T for over 15 years. I spend most of my time working with Microsoft Powershell in an effort to find creative ways to manage the data on our file shares. I’ve found Powershell to be a useful, and interesting way to perform Sys. Admin functions.

7 Responses to Getting the Directory Size on Remote Servers.

  1. kenny says:

    Thanks! Just getting the hang of the smaller commands in Powershell. Trying to make the leap to more complex operations can be daunting. I appreciate the help!

  2. Rena says:

    Too many compliments too little space, thanks!

  3. Mark Leddy says:

    Great script. Thanks.

    To get it running I changed the qoutes. Also I changed to GB rather then MG.

    I noticed the sort for me sorted alphabetically, so for me it sorted in the following order:

    0.99 GB
    1.00 GB
    11.05 GB
    9.67 GB

    I guess the string get sent to the
    If anyone finds a way to get this truely sorted by size please let me know.


  4. Robert Lamar says:

    I tried your on line script in my environment while is a microsoft failover cluster environment. The information it return is missing the sum.

    Here’s what it look like: count
    Sum: 0
    Property: lenght

    I am using your command: get-ChildItem \\xxxxxx\n$\#######\myname -recurse -force |Select lenght |Measure-Object -Sum Lenght

    In the gui property I have 519 MBs

    • Patrick says:

      Hello Robert
      There looks like a typo with the Length property in your Select-Object call.

      Try it with .length instead.

      get-ChildItem \\xxxxxx\n$\#######\myname -recurse -force |Select LENGTH|Measure-Object -Sum LENGTH
      Thank You


  5. William says:

    Had to change $dirSize.sum = $dirSize.sum/1MB to $dirSize.sum = ($dirSize.sum/1MB) for it to work, otherwise the size was not reported.

    By the way, it would be nice to “spice” it up a bit to include the full UNC path of $path, e.g. the folder that I want to get the size on contains various random user folders where the data I’m intersted is being stored.

  6. Bastiaan says:

    Changed “$finalResult = “{0:N2} MB” -f $result.sum” to “$finalResult = “{0:N2} MB” -f $dirsize.sum” because you want the sum of dirsize. The “result”isn’t there so you can’t sum it.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: