Search code examples
powershellformatwmi

How can I show size in GB for WMI output?


I wrote a script for formatting disk information using WMI, with the following command and syntax:

Get-WMIObject -ComputerName $Computer -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}

But I need format the output to express the size in GB. How handle the output?

Thanks!


Solution

  • You can use expressions to format your output with Select-Object:

    For example, you can write that:

    $Computer = 'localhost'
    $DiskInfo = Get-WMIObject -ComputerName $Computer -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3}  `
    | Select-Object @{Name="Device";Expression={($_.Name)}},
                    @{Name="Size";Expression={([math]::Round($_.size/1gb))}},
                    @{Name="Free";Expression={([math]::Round($_.freespace/1gb))}}
    

    I select the properties and format using the math method Round (Math.Round Method). With the following output:

    PS C:\Users\vmsilvamolina> $DiskInfo
    
    Device Size Free
    ------ ---- ----
    C:      223   23