Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding properties in powershell

Tags:

powershell

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!

like image 990
Jeff S. Avatar asked Oct 29 '25 01:10

Jeff S.


2 Answers

 $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
like image 138
Ranadip Dutta Avatar answered Oct 31 '25 15:10

Ranadip Dutta


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.

like image 43
Peter Hahndorf Avatar answered Oct 31 '25 17:10

Peter Hahndorf