How To Find Disk Capacity and Free Space of Remote Computers

16

Finding disk capacity and free space of a local computer is easy but not so easy from a remote computer, especially through a GUI interface. It’s much easier to utilize the power of PowerShell and here is how you can do it.

Get-PSDrive is a native PowerShell cmdlet that lists all storage drives on your local system.

image 23 - How To Find Disk Capacity and Free Space of Remote Computers

You can narrow down to only list the file systems by piping out the result to a Where clause.

Get-PSDrive | Where {$_.Free -gt 0}
image 24 - How To Find Disk Capacity and Free Space of Remote Computers

Since the cmdlet doesn’t have a -ComputerName switch to access remote computers, we need Invoke-Command to run the cmdlet on a remote computer.

Invoke-Command -ComputerName remote_computer {Get-PSDrive | Where {$_.Free -gt 0}}
image 25 - How To Find Disk Capacity and Free Space of Remote Computers

This works pretty well only when you have WinRM and PSRemoting enabled on the remote computers. And that’s why I like the Get-WmiObject method even better.

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, FreeSpace, Size
image 27 - How To Find Disk Capacity and Free Space of Remote Computers

To list the size in GB format, 

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[math]::truncate($_.size / 1GB)}}, @{'Name'='Freespace (GB)'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}
image 28 - How To Find Disk Capacity and Free Space of Remote Computers

How about display the result with the thousands separators?

Get-WmiObject Win32_LogicalDisk -ComputerName remote_computer -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}
image 29 - How To Find Disk Capacity and Free Space of Remote Computers

You can access multiple remote computers from one run by putting all of them after the -ComputerName switch, separated by a comma.

Get-WmiObject Win32_LogicalDisk -ComputerName computer1,computer2,computer3 -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}
image 30 - How To Find Disk Capacity and Free Space of Remote Computers

Also, here is the script I put together that has a better formatted output.

$servers = @("computer1", "computer2", "computer3")

Foreach ($server in $servers)
{
    $disks = Get-WmiObject Win32_LogicalDisk -ComputerName $server -Filter DriveType=3 | 
        Select-Object DeviceID, 
            @{'Name'='Size'; 'Expression'={[math]::truncate($_.size / 1GB)}}, 
            @{'Name'='Freespace'; 'Expression'={[math]::truncate($_.freespace / 1GB)}}

    $server

    foreach ($disk in $disks)
    {
        $disk.DeviceID + $disk.FreeSpace.ToString("N0") + "GB / " + $disk.Size.ToString("N0") + "GB"

     }
 }

/Update on Dec 9, 2019/

Note that moving forward, future PowerShell versions no longer support Get-WmiObject so if all of a sudden you see the error message like “RPC Server is unavailable”, it’s probably time to switch over to Get-CimInstance instead. The parameters are the same for Win32_LogicalDisk. Simply replace Get-WmiObject with Get-CimInstance and you are good to go.

SHARE

16 COMMENTS

  1. This is simply a genious script! Saves me a bunch of work having to log into all of my 12 VM’s all the time to check disk capacity. Thanks a lot!

  2. This is an awesome script. I have a question on how you would read computer names from a txt file or .csv in place of “computer1”, “Computer2”, “Computer3”. Also how do you output the data to a csv in three columns (Computer name, free diskspace, disk size.)

    This way we could sort by computers that have the least amount of disk space.

  3. Is there a way of getting a total of all available disks? For instance, I don’t care if there are 15 drives each 1 GB all I want to see is that I have 15GB total.

LEAVE A REPLY

Please enter your comment!
Please enter your name here