Manipulating Object

Welcome Forums General PowerShell Q&A Manipulating Object

This topic contains 2 replies, has 3 voices, and was last updated by

3 years, 5 months ago.

  • Author
  • #32653

    Topics: 28
    Replies: 80
    Points: 0
    Rank: Member
    $memory   = Get-WmiObject -ComputerName $computerName  win32_physicalmemory
     RAMtotal =  (($memory.capacity | Measure-Object -sum).sum)/1gb

    I am trying to format this with two number to the right of the decimal. "N0 {0.00} formatted" the values I have are objects not INT. How do I clean this up?

  • #32655

    Topics: 1
    Replies: 110
    Points: 82
    Rank: Member

    To use formatting you can use the -f operator but bear in mind that this only returns strings so sorting may not work as you'd expect. Here's an example:

    "{0:N2}" -f [Math]::Pi

    This takes the values to the right of the -f operator, inserts it into the string "template" at position 0 (denoted by the first 0) and formats it as a number with 2 decimal places (N2)

  • #32657

    Topics: 6
    Replies: 650
    Points: 2
    Rank: Member

    Use the Math.Round method if you are trying to crop off lots of extra decimal values, but if you are just trying to always have 2 decimal places, even if it's just a whole number, Peter's got your answer.

    $memory   = Get-WmiObject win32_physicalmemory
    $RAMtotal =  [Math]::Round((($memory.capacity | Measure-Object -sum).sum)/1gb, 2)

The topic ‘Manipulating Object’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort