Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Calculated properties

11 views
Skip to first unread message

Marco Shaw [MVP]

unread,
Jan 4, 2008, 7:11:15 PM1/4/08
to
Can I use the division operator when doing calculated properties?

This does not work for me:
PSH>get-wmiobject win32_logicaldisk|where-object {$_.DriveType -eq
3}|format-table @{expression={$_.FreeSpace/$_.Size};label="% Free"}

I tried a few different things, but can't seem to get it to work.

--
Microsoft MVP - Windows PowerShell
http://www.microsoft.com/mvp

PowerGadgets MVP
http://www.powergadgets.com/mvp

Blog:
http://marcoshaw.blogspot.com

Keith Hill [MVP]

unread,
Jan 4, 2008, 9:27:58 PM1/4/08
to
"Marco Shaw [MVP]" <marco.shaw@_NO_SPAM_gmail.com> wrote in message
news:#mfd#9yTIH...@TK2MSFTNGP04.phx.gbl...

> Can I use the division operator when doing calculated properties?
>
> This does not work for me:
> PSH>get-wmiobject win32_logicaldisk|where-object {$_.DriveType -eq
> 3}|format-table @{expression={$_.FreeSpace/$_.Size};label="% Free"}
>
> I tried a few different things, but can't seem to get it to work.
>

This has come up before - trouble using the standard arithmetic operators on
UInt64. Unless your hard drives are "out of this world" large, you could
just cast to [long] before dividing:

gwmi win32_logicaldisk | ?{$_.DriveType -eq 3} | ft
@{e={[long]$_.FreeSpace/[long]$_.Size};l="% Free"}

--
Keith

MichielV

unread,
Jan 5, 2008, 6:41:01 AM1/5/08
to

"Keith Hill [MVP]" wrote:

Don't forget to multiply by 100 :-)


gwmi win32_logicaldisk | ?{$_.DriveType -eq 3} | ft

@{e={ (([long]$_.FreeSpace/[long]$_.Size)*100)};l="% Free"}

Shay Levi

unread,
Jan 5, 2008, 3:47:01 PM1/5/08
to

If performance matters, WMI is way more slower in comparison to the DriveInfo
class.
I selected the TotalMilliseconds only, you can see the difference.

measure-command {get-wmiobject win32_logicaldisk | where {$_.DriveType -eq
3} | ft @{l="% Free";e={([long]$_.FreeSpace/[long]$_.Size)*100}}}

TotalMilliseconds : 1822.5704


# you can use the -filter parameter and avoid the where clause, much better
measure-command { get-wmiobject win32_logicaldisk -filter "DriveType=3" |
ft @{l="% Free";e={([long]$_.TotalFreeSpace/[long]$_.TotalSize)*100}}}

TotalMilliseconds : 770.2297


# no -filter parameter available here but its a winner.
measure-command {[System.IO.DriveInfo]::GetDrives() | where {$_.DriveType
-eq "Fixed"} | ft @{l="% Free";e={([long]$_.TotalFreeSpace/[long]$_.TotalSize)*100}}}

TotalMilliseconds : 2.9774

BTW, this won't require you to remember to *100 :)

"{0:P2}" -f ([long]$_.TotalFreeSpace/[long]$_.TotalSize)


-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic

> Can I use the division operator when doing calculated properties?
>
> This does not work for me:
>
PSH>> get-wmiobject win32_logicaldisk|where-object {$_.DriveType -eq

PSH>>

> 3}|format-table @{expression={$_.FreeSpace/$_.Size};label="% Free"}
>
> I tried a few different things, but can't seem to get it to work.
>

IT Staff

unread,
Jan 6, 2008, 9:13:00 PM1/6/08
to
i realised if i stored the whole strings into a variable, it takes only 0.09
miliseconds

eg

measure-command { gwmi win32_logicaldisk | where {$_.drivetype -eq 3} | ft
@{e={([long]$_.freespace/[long]$_.size)*100}}}

==> 130 totalmilliseconds

$a = gwmi win32_logicaldisk | where {$_.drivetype -eq 3} | ft
@{e={([long]$_.freespace/[long]$_.size)*100}}

measure-command {$a}

==> 0.09 totalmiliseconds.

Pls share thoughts :-)


"Shay Levi" <n...@addre.ss> wrote in message
news:8766a944176358...@news.microsoft.com...

Keith Hill [MVP]

unread,
Jan 7, 2008, 12:49:36 AM1/7/08
to
"IT Staff" <jkk...@hotmail.com> wrote in message
news:O3OQYLNU...@TK2MSFTNGP04.phx.gbl...

> i realised if i stored the whole strings into a variable, it takes only
> 0.09 miliseconds
>
> eg
>
> measure-command { gwmi win32_logicaldisk | where {$_.drivetype -eq 3} | ft
> @{e={([long]$_.freespace/[long]$_.size)*100}}}
>
> ==> 130 totalmilliseconds
>
>
>
> $a = gwmi win32_logicaldisk | where {$_.drivetype -eq 3} | ft
> @{e={([long]$_.freespace/[long]$_.size)*100}}
>
> measure-command {$a}
>
> ==> 0.09 totalmiliseconds.
>
>
>
> Pls share thoughts :-)

Because this line does the work:

$a = gwmi win32_logicaldisk | where {$_.drivetype -eq 3} | ft
@{e={([long]$_.freespace/[long]$_.size)*100}}

In your second measure-command, you are just measuring the amount of time it
takes PowerShell to render the formatting objects stored in $a.

--
Keith

IT Staff

unread,
Jan 7, 2008, 12:58:30 AM1/7/08
to
ok, since in the scripting world, shld i put in as variable ? eg the one
below $a ?


"Keith Hill [MVP]" <r_keit...@mailhot.moc.no_spam_I> wrote in message
news:8886B935-B201-41EC...@microsoft.com...

IT Staff

unread,
Jan 7, 2008, 1:02:09 AM1/7/08
to
sorry i mean that if i put my command into variable $a, does any execution
takes place within $a itself ?


"Keith Hill [MVP]" <r_keit...@mailhot.moc.no_spam_I> wrote in message
news:8886B935-B201-41EC...@microsoft.com...

Shay Levi

unread,
Jan 7, 2008, 3:14:27 AM1/7/08
to
Execution time is the same even when assigning it to a variable.

Also, your calculated property is missing the label key:
... | ft @{e={([long]$_.freespace/[long]$_.size)*100}}

the result looks like this:

([long]$_.freespace/[long]$_.size)*100

--------------------------------------
9.78514352109411
14.8044774084049

> sorry i mean that if i put my command into variable $a, does any

0 new messages