4

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!

5
  • I'm surprised how there is so much online content on how to get the drive properties including Get-WmiObject but there is no information on how to get the total size whatsoever! Commented Mar 4, 2017 at 5:10
  • Welcome to Stack Overflow. Please have a look How do I write a good question for suggestions on maximizing the audience your question reaches. Specifically, I might recommend revising your title to include your actual question, and revising the question body to include the anticipated result. On the latter, you're almost there but it's not quite clear whether you're trying to display two values, perform the math and display one value, store a variable, etc. Good luck! Commented Mar 4, 2017 at 5:44
  • @FissureKing: May i know which part of the questions is not clear? He clearly mentioned that he want to add them together, since he is getting free and used space already. Commented Mar 4, 2017 at 7:10
  • @RanadipDutta specifically, does he/she expect this (or corrected) syntax to return the added value? or should this command be piped into a function that handles adding? where is the bottleneck? is this a string manipulation issue, or a general powershell question? Commented Mar 4, 2017 at 7:13
  • If u have run the command , then you would have understood this part that he wants to achieve the total disk size,which by default is not there. So he want to sum the Used and Free size to get it done. Its pretty straight forward. Now I dont know how come function,string manipulation coming in picture. Commented Mar 4, 2017 at 7:16

3 Answers 3

5
 $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
Sign up to request clarification or add additional context in comments.

3 Comments

what does the -f do in the: e = {"{0:N2}" -f ($_.used/1GB)}}
@JeffS.: Using .NET to Format Numbers in Windows PowerShell
Go through the link for more understanding on this.Formatting tips
4

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.

1 Comment

Thanks! If I didn't name it add properties I would vote this as the correct answer since it is the easiest and shortest solution
0
#solution 1 with new column into select
Get-Volume -DriveLetter C| 
select @{n= 'Used' ; e = {"{0:N2}" -f (($_.Size - $_.SizeRemaining)/1GB)}}, @{n= 'Free' ; e = {"{0:N2}" -f ($_.SizeRemaining/1GB)}}, @{n= 'Total' ; e = {"{0:N2}" -f ($_.Size/1GB)}}

#solution 2 with foreach
Get-Volume -DriveLetter C| foreach {
[pscustomobject]@{
Used="{0:N2}" -f (($_.Size - $_.SizeRemaining)/1GB)
Free="{0:N2}" -f ($_.SizeRemaining/1GB)
Total="{0:N2}" -f ($_.Size/1GB)
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.