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.
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}
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}}
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
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)}}
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))}}
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))}}
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.
hi
How do I make this command run on a local machine.
Take up free space, total
-ComputerName 127.0.0.1
you’re a genius – thank you!
Hi, how can I specify a different account to connect to the servers?
Oh, thank you so much. This was exactly what I was looking for.
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!
Thanks. I also scheduled it to run each morning and use Send-MailMessage to email me the result so I don’t have to do it manually. a bit more automated.
Hi kent,
how can i send output to mail using the same script what need to be added.
Regards,
Sowmith
Can we add the server name for each listed drive ?
e.g.
Size(GB) FreeSpace(GB)
Server1 C: 100 10
Yes, sure. Just put $server + in front of $disk.DeviceID.
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.
Do you know how I could export this output to CSV. All I seem to get is the length of the array
What if I want to get that info from VMs that are on different (private networks) and have authentication?
How can we check the size in more specific way?
like if the actual drive capacity or free space is 7.95GB it just shows 7GB.
What will be the code if I want to find “In use” space i.e
In Use= size – size remaining
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.
Hello I need to add one more column Free% as well for the drive please can you help
For the last question : Freespace/Size*100
Great script…. But how can you get it to separate the drives by the computer they are on? Because the way the output reads now I can’t tell what drive belongs to which computer.
It displays the computer’s name first, followed by the list of drivers that belongs to that computer.
Thank you so much for this! Can you also please show me how I can add another column that shows Freespace (%)
Thank you again!