Rakha
Rakha

Reputation: 2064

Doing math on a piped property powershell

Very new to doing math on properties... I'm trying :

Get-WMIObject win32_diskdrive -computer $poste | Add-Member -MemberType ScriptProperty -Name  GB -Value {[int]($_.size/1GB)} -PassThru |  select model, size,GB

and I think i'm referecing the SIZE property wrong because I get :

model                                            size GB
-----                                            ---- --
Corsair Neutron XTI SSD SCSI Disk Device 480101368320  0
ST350041 3AS SCSI Disk Device            500105249280  0

What am I doing wrong? Thank you

Upvotes: 0

Views: 1311

Answers (3)

Chenry Lee
Chenry Lee

Reputation: 380


This expression is a bit simpler:

Get-WMIObject win32_diskdrive | select model, size,@{n="GB";e={[int]($_.size/1GB)}} 

And it works as well.

Upvotes: 3

jrider
jrider

Reputation: 1640

You can use an Expression in your select statement to calculate size in GB.

Ex:

Get-WMIObject win32_diskdrive -computer $poste  | Select  @{Label = "Model";Expression = {$_.Model}},
        @{Label = "Size";Expression = {"{0:N1}" -f($_.Size) }},
        @{Label = "Total Capacity (GB)";Expression = {"{0:N1}" -f( $_.Size / 1gb)}} #This will devide size by 1gb

Upvotes: 2

EBGreen
EBGreen

Reputation: 37730

Do it as an explicit ForEach-Object:

Get-WMIObject win32_diskdrive  | %{Add-Member -Input $_ -MemberType ScriptProperty -Name  GB -Value {[int]($_.size/1GB)} -PassThru} |  select model, size,GB

Upvotes: 1

Related Questions