Solved

Need Help formatting Powershell value

Posted on 2014-01-28
4
342 Views
Last Modified: 2014-01-28
So this is the phrase I'm working with

$wmi = Get-WmiObject Win32_OperatingSystem -Computer $computername
	$bootime = $wmi | Select @{Name="LastBoot";Expression={$_.ConvertToDateTime($_.LastBootupTime)}},
        @{Name="OS";Expression={$_.Caption}},
        @{Name="ServicePack";Expression={$_.CSDVersion}},
        @{Name="Uptime";Expression={(Get-Date) - ($_.ConvertToDateTime($_.LastBootupTime))}}, 
        @{Name="Total Memory";Expression= {$_.TotalVisibleMemorySize}} | ConvertTo-HTML -Fragment -PreContent "<h2>LastBoot Time</h2>" |Out-String	

Open in new window



The one I'm having a problem with is the total physical memory
{Name="Total Memory";Expression= {$_.TotalVisibleMemorySize}}

Currently it comes out with a value of 8255992, that's really 8GB but most people wouldn't know that.  How do I format the value in a more user friendly value?


thanks
0
Comment
Question by:c7c4c7
  • 2
4 Comments
 
LVL 40

Accepted Solution

by:
Subsun earned 250 total points
Comment Utility
Try.
@{Name="Total Memory";Expression= {"$([Math]::Round($_.TotalVisibleMemorySize /1MB,0))GB"}}

Open in new window

0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Luckily PS comes with some nice conversion options.  For sizes you have to decide how you want things displayed, either in GB, KB, the number of decimal points, etc.  In the example below, I'm showing in GB, and using the format operator (-f) to help with specifying two decimal places and the "GB" after the number.  
@{Name="Total Memory";Expression= {"{0:N2} GB" -f ($_.TotalVisibleMemorySize/1GB)}}

Open in new window


A link to help explain the formatting.
http://technet.microsoft.com/en-us/library/ee692795.aspx
0
 

Author Closing Comment

by:c7c4c7
Comment Utility
Thanks for your help, works perfectly.

Footech thanks for your help your solution came up with a value of .01GB
0
 
LVL 39

Expert Comment

by:footech
Comment Utility
Ah, interesting, I missed that the size is already reported in MB instead of bytes.  In that case just change the "1GB" to "1MB" and it works.  But so does Subsun's (it's just rounded to zero decimal places instead of two).
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This article shows how a content item can be identified directly or through translation of a navigation type. It then shows how this information can be used to create a menu for further navigation.
This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now