Solved

Need Help formatting Powershell value

Posted on 2014-01-28
4
358 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 250 total points
ID: 39815625
Try.
@{Name="Total Memory";Expression= {"$([Math]::Round($_.TotalVisibleMemorySize /1MB,0))GB"}}

Open in new window

0
 
LVL 40

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 40

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

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

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

Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
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…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

695 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