1

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!

2 Answers 2

3

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
2
  • Wow! Thanks for the quickly response! Jan 12, 2018 at 12:59
  • FYI. These are called calculated properties.
    – Matt
    Jan 12, 2018 at 13:11
0

You can customize the output like this:

Get-WMIObject -Class Win32_LogicalDisk | Where-Object {$_.DriveType -eq 3} | 
    Select DeviceID,DriveType,VolumeName,@{N="Size";E={"{0:N2}" -f ($_.Size / 1GB)}},@{N="FreeSpace";E={"{0:N2}" -f ($_.FreeSpace / 1GB)}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.