?
Solved

Need Help formatting Powershell value

Posted on 2014-01-28
4
Medium Priority
?
366 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 40

Accepted Solution

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

Open in new window

0
 
LVL 41

Expert Comment

by:footech
ID: 39815654
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
ID: 39815807
Thanks for your help, works perfectly.

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

Expert Comment

by:footech
ID: 39815877
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A procedure for exporting installed hotfix details of remote computers using powershell
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

650 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