locked
Concatenate text after math? RRS feed

  • Question

  • Trying to get text to go after the math and show a better output.

    Clear-Host

    gwmi win32_logicaldisk -namespace "root\CIMV2"| Where-Object {$_.DriveType -eq 3} | Format-Table DeviceId, VolumeName, @{n="Size";e={[math]::` 

    Round($_.Size/1GB)}},@{n="FreeSpace";e={[math]::Round($_.FreeSpace/1GB)}}, @{n="% Free";e={[math]::Round($_.FreeSpace/$_.Size,2)*100}} 

    Shows

    I would like to show with GB and % and if possible center the column under the header.

    Example:


    -- Adam


    Wednesday, March 21, 2018 5:20 PM

Answers

  • You must have done something wrong. I don't get any errors:

    gwmi win32_logicaldisk -namespace "root\CIMV2"| 
        Where-Object {$_.DriveType -eq 3} | 
        Format-Table DeviceId, VolumeName, 
            @{n="Size";e={[math]::Round($_.Size/1GB)}},
            @{n="FreeSpace";e={"{0} GB" -f ([math]::Round($_.FreeSpace/1GB))}},
            @{n="% Free";e={[math]::Round($_.FreeSpace/$_.Size,2)*100}} 

    Output:

    DeviceId VolumeName      Size FreeSpace % Free
    -------- ----------      ---- --------- ------
    C:                        153 19 GB         13
    E:                         14 2 GB          12


    Thursday, March 22, 2018 8:47 PM

All replies

  • "{0:N2} GB" -f ([math]::Round($_.FreeSpace/1GB))
    • Proposed as answer by PRASOON KARUNAN V Wednesday, March 21, 2018 7:25 PM
    • Unproposed as answer by Adam F Kings Thursday, March 22, 2018 8:04 PM
    Wednesday, March 21, 2018 6:45 PM
  • With format-table, you can have a calculated property have an alignment, but it seems like a lot of trouble.


    ls | ft @{l='Length';e={$_.length};a='center'}

    Length
    ------
    1
    8



    • Edited by JS2010 Wednesday, March 21, 2018 7:08 PM
    Wednesday, March 21, 2018 7:08 PM
  • This one is causing an error I did not get before.

    -- Adam

    Thursday, March 22, 2018 8:03 PM
  • Hi,

    Instead of doing like it make it in the headings like

    Total Size (GB) | Free Space (GB) | % Free space (GB)

    Do you have any special requirement like it has to be next to the value?

    Thanks

    Syed


    Dont forget to mark as Answered if you found this post helpful.


    • Edited by Syed Abdul Thursday, March 22, 2018 8:13 PM
    Thursday, March 22, 2018 8:12 PM
  • You must have done something wrong. I don't get any errors:

    gwmi win32_logicaldisk -namespace "root\CIMV2"| 
        Where-Object {$_.DriveType -eq 3} | 
        Format-Table DeviceId, VolumeName, 
            @{n="Size";e={[math]::Round($_.Size/1GB)}},
            @{n="FreeSpace";e={"{0} GB" -f ([math]::Round($_.FreeSpace/1GB))}},
            @{n="% Free";e={[math]::Round($_.FreeSpace/$_.Size,2)*100}} 

    Output:

    DeviceId VolumeName      Size FreeSpace % Free
    -------- ----------      ---- --------- ------
    C:                        153 19 GB         13
    E:                         14 2 GB          12


    Thursday, March 22, 2018 8:47 PM
  • The last 3 columns centered:


    gwmi win32_logicaldisk -namespace "root\CIMV2"|
    Where-Object {$_.DriveType -eq 3} |
    Format-Table DeviceId, VolumeName,
    @{n="Size";e={[math]::Round($_.Size/1GB)};a='center'},
    @{n="FreeSpace";e={"{0} GB" -f ([math]::Round($_.FreeSpace/1GB))};a='center'},
    @{n="% Free";e={[math]::Round($_.FreeSpace/$_.Size,2)*100};a='center'}

    DeviceId VolumeName Size FreeSpace % Free
    -------- ---------- ---- --------- ------
    C: 119 16 GB 13


    • Edited by JS2010 Thursday, March 22, 2018 8:54 PM
    • Proposed as answer by Leif-Arne Helland Thursday, March 22, 2018 9:44 PM
    Thursday, March 22, 2018 8:53 PM
  • Thanks, and yes, I had an extra space there which gave me an error, thank you for the help guys, new lesson learned

    -- Adam

    Thursday, March 22, 2018 11:09 PM
  • Hi,

    I'm checking how the issue is going, was your issue resolved?

    And if the replies as above are helpful, we would appreciate you to mark them as answers, and if you resolve it using your own solution, please share your experience and solution here. It will be greatly helpful to others who have the same question.

    Appreciate for your feedback.

    Best Regards,
    Albert

    Please remember to mark the replies as an answers if they help.
    If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com

    Friday, March 23, 2018 1:20 AM