?
Solved

Need Help formatting Powershell value

Posted on 2014-01-28
4
Medium Priority
?
364 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 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this post we will be converting StringData saved within a text file into a hash table. This can be further used in a PowerShell script for replacing settings that are dynamic in nature from environment to environment.
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…

764 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