header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

Another formatting question
Last Post 20 Sep 2008 03:33 PM by Elan. 3 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
20 Sep 2008 01:34 PM  

Get-WmiObject Win32_LogicalDisk | Format-Table -autosize DeviceID,@{Label="Size(G)";Expression={[math]::round($_.Size/1GB,2)}},@{Label="Freespace(G)";Expression={[math]::round($_.FreeSpace/1GB,2)}},@{Label="Percent";Expression={"{0:P2}" -f ($_.Size/1GB - ($_.Size/1GB - $_.Freespace/1GB) / $_.Size/1GB)}}

 

All you need to look at is the last Expression statement.  For some reason, it's coming back as:

 

DeviceID Size(G) Freespace(G) Percent
-------- ------- ------------ -------
C:         93.16        54.73 9,315.82 %
D:        257.71       202.51 25,770.63 %

I understand Order of Operations is causing itn to show as 9,315% but anytime I try taking away paranthesis or adding some to ensure the first part of the calculation gets calcualted first, nothing ends up showing in Percent.

Any ideas?

Elan Shudnow
http://www.shudnow.net
ShayUser is Online
Basic Member
Basic Member
Posts:281
Avatar

--
20 Sep 2008 02:36 PM  
There is no need to do the full percentage math when you are formatting with the "P" format pattern, the value is multiplied by 100 and displayed as a percentage:


# freespace percentage
"{0:P2}" -f ($_.freeSpace/$_.size)
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
20 Sep 2008 03:15 PM  
It's odd because if I do the following, I get $null in Percent:
@{Label="Percent";Expression={"{0:P2}" -f ($_.FreeSpace / $_.Size)}}

If I do the following, I get 0.00% in Percent:
@{Label="Percent";Expression={"{0:P2}" -f ($_.FreeSpace/1GB / $_.Size/1GB)}}
Elan Shudnow
http://www.shudnow.net
ElanUser is Offline
New Member
New Member
Posts:28
Avatar

--
20 Sep 2008 03:33 PM  
Nevermind, got it working. Thanks Shay.

@{Label="Percent";Expression={"{0:P2}" -f (($_.FreeSpace/1GB) / ($_.Size/1GB))}}
Elan Shudnow
http://www.shudnow.net
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer