I want to add C drive properties (free and used space) in order to get the total size.
Get-PSDrive C | Select-Object -Property Free,Used
This Shows the free and used space of drive C. I am able to convert them in GB using customized properties but not sure how to add those properties together. Any help will be appreciated. Thanks!
$computerHDDs= Get-WmiObject Win32_LogicalDisk -Filter "DeviceID='C:'"
$logicalDisks = @()
foreach($item in $computerHDDs)
{
$logicalDisk =[ordered]@{
Name=$item.DeviceID -replace ':' ,'';
DiskSize = "{0:N2}" -f ($item.Size/1GB) + " GB" ;
}
$logicalDisks +=$logicalDisk
}
$logicalDisks
Even in psdrive if you are able to get the free and used, then summation of both should give you the total size
In your case, you can do like this:
$hdd= Get-PSDrive C | Select-Object -Property Free,Used
$total= (($hdd.free + $hdd.Used)/1GB).ToString() + " GB"
$total
Note: If you want to get the value only, then remove the tostring method and the GB part. Use the first portion only
And for all drives, you can use like this:
Get-PSDrive -PSProvider filesystem | select Name, @{n= 'Used(GB)' ; e = {"{0:N2}" -f ($_.used/1GB)}}, @{n= 'Free (GB)' ; e = {"{0:N2}" -f ($_.Free/1GB)}}, @{n= 'Total(GB)' ; e = {"{0:N2}" -f (($_.used + $_.Free)/1GB)}} | Format-Table -AutoSize
PSDrive is not the best object to work with if you want full information on a hard drive partition, use the Volume cmdlets instead:
(Get-Volume -DriveLetter C).Size
or
Get-Volume -DriveLetter C | Select-Object Size
A PSDrive is an abstraction in PowerShell to treat various types of objects in a similar way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With