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  

       $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.

Patrick

7 thoughts on “Getting the Directory Size on Remote Servers.

  1. 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. 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.

    Thanks,
    Mark

  3. 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
    Average
    Sum: 0
    Maxium
    Minimum
    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

    1. 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

      Patrick

  4. 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.

  5. 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 comment