locked
How Do I Format Numbers in Simple Hashtable RRS feed

  • Question

  • Here's what I'm running (below).  I just want the Used number to have nice comma formatting.

    Get-PSDrive -PSProvider FileSystem | Select-Object -Property Name, Root, @{Label = 'Used'; Expression = "'{0:N0}' -f Used"}, Free, Description | ft -AutoSize

    Output:

    Name Root Used          Free Description

    ---- ---- ----          ---- -----------

    C    C:\       2080995323904 System

    D    D:\       2824702590976 FileShares

    E    E:\        138379440128 DSL/Spool

     

    As you can see, '{0:N0}' -f Used" is apparently resolving null.

     

    Expected Output:

    Name Root              Used          Free Description

    ---- ----              ----          ---- -----------

    C    C:\    118,027,239,424 2080987623424 System

    D    D:\  8,186,720,989,184 2823327006720 FileShares

    E    E:\    847,281,995,776  138379440128 DSL/Spool

     

    What's going wrong here ?  How can I generate the expected output using the '{0:N0}' -f 123456 method ?


    -- huddie71 ~~If you~re not seeking help or offering it, you probably shouldn~t be here.~~

    Tuesday, June 12, 2018 10:22 AM

All replies

  • You could do it like this:

    Get-PSDrive -PSProvider FileSystem | Select-Object -Property Name, Root, Used, Free, Description | ForEach-Object -Process {$_.Used = '{0:N0}' -f $_.Used; $_} | ft -AutoSize


    • Edited by TobyU Tuesday, June 12, 2018 10:30 AM
    Tuesday, June 12, 2018 10:30 AM
  • You could do it like this:

    Get-PSDrive -PSProvider FileSystem | Select-Object -Property Name, Root, Used, Free, Description | ForEach-Object -Process {$_.Used = '{0:N0}' -f $_.Used; $_} | ft -AutoSize



    That's cool! You're modifying the Used property of the object then formatting it, in a foreach-object code block.  That does the trick perfectly!  Thanks for answering so quickly too.  

    -- huddie71 ~~If you~re not seeking help or offering it, you probably shouldn~t be here.~~

    Tuesday, June 12, 2018 10:56 AM
  • With the correct syntax you'll get the expected results ... ;-)

    Get-PSDrive -PSProvider FileSystem | 
        Select-Object -Property Name, 
                                Root, 
                                @{Name = 'Used'; Expression = { "{0:N0}" -f $_.Used}}, 
                                @{Name = 'Free'; Expression = { "{0:N0}" -f $_.Free}}, 
                                Description | 
                                    Format-Table -AutoSize
    But you should keep in mind that these numbers are actually not numbers anymore ...  you couldn't calculate with them - they're strings.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Tuesday, June 12, 2018 11:11 AM
    Tuesday, June 12, 2018 11:09 AM