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
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
"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"}
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.
>
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...
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
"Keith Hill [MVP]" <r_keit...@mailhot.moc.no_spam_I> wrote in message
news:8886B935-B201-41EC...@microsoft.com...
"Keith Hill [MVP]" <r_keit...@mailhot.moc.no_spam_I> wrote in message
news:8886B935-B201-41EC...@microsoft.com...
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
-----
Shay Levi
$cript Fanatic
http://scriptolog.blogspot.com
Hebrew weblog: http://blogs.microsoft.co.il/blogs/scriptfanatic
> sorry i mean that if i put my command into variable $a, does any