Solved

Need Help formatting Powershell value

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

Open in new window

0
 
LVL 39

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 39

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

This script checks a path to see if a folder exists. If the folder does exist you will get output "The folder has previously been created. No action taken" If not it will create the folder. Then adds one user modify permission to the folder. It …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

786 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