Get-WmiObject win32_logicaldisk doesn't display 'Gb'

This topic contains 4 replies, has 3 voices, and was last updated by Profile photo of Richard Siddaway Richard Siddaway 2 weeks, 1 day ago.

  • Author
    Posts
  • #74862
    Profile photo of Gururaj Padaki
    Gururaj Padaki
    Participant

    Hi Folks,

    Need your expert advice here.

    Below is the code that I wrote to get the Size and Free space on C and E drives from multiple servers which I would input via a text file.

    $Computers = Get-Content -Path c:\temp\servers.txt
    
        foreach ($Computer in $Computers)
    
            {
                Get-WmiObject -ComputerName $Computer -Class win32_logicaldisk |
                where {$_.DeviceID -eq "C:" -or $_.DeviceID -eq "E:"} | 
                Select Pscomputername, DeviceID, @{n='Size(GB)'; e={($_.Size/1gb -as [int])}}, @{n='Free(GB)'; e={($_.FreeSpace/1GB) -as [int]}}
            }
    

    Below is the output that I receive.

    PSComputerName DeviceID Size(GB) Free(GB)
    -------------- -------- -------- --------
    Server1        C:             60       31
    Server2        C:             50       30
    Server3        E:             40       18
    
    

    Now, I want the Size and Free Space values (Eg. 60 and 31 in first line) to have 'Gb' string with them (Eg. 60Gb and 31Gb).
    How to get the output in that format?

  • #74866
    Profile photo of DanT
    DanT
    Participant
        
    foreach ($Computer in $Computers)
    
            {
                Get-WmiObject -ComputerName $Computer -Class win32_logicaldisk |
                where {$_.DeviceID -eq "C:" -or $_.DeviceID -eq "E:"} | 
                Select Pscomputername, DeviceID, @{n='Size(GB)'; e={("$($_.Size/1gb -as [int])GB")}}, @{n='Free(GB)'; e={("$($_.FreeSpace/1gb -as [int])GB")}}
            }
    
    • #74875
      Profile photo of Gururaj Padaki
      Gururaj Padaki
      Participant

      Thank you so much DanT.
      That worked..! It would be great if you explain me the logic you put those parans () and double quotes ".
      I was kind of struggling by putting them here and there, in the same line.

      Thanks in Advance.

    • #74898
      Profile photo of DanT
      DanT
      Participant

      Hi there,

      It's just using a subexpression '$()' and then turning the whole thing into a string whilst also adding GB on the end.

      Dan

  • #74908
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    The drawback to doing that is that you're turning a numeric value into a string. This could make further processing of your output objects more difficult. Its generally better to set the unit size in the property name and leave the value as a straight numeric

You must be logged in to reply to this topic.