Get disk space - %Free column

Hello,

Using the following script, i could get the space and usage columns.

what should i add to it to get a %free column instead of calculate it manually?

Get-WmiObject Win32_LogicalDisk -ComputerName ComputerName -Filter DriveType=3 | Select-Object DeviceID, @{'Name'='Size (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.size / 1GB))}}, @{'Name'='Freespace (GB)'; 'Expression'={[string]::Format('{0:N0}',[math]::truncate($_.freespace / 1GB))}}

What do you mean with “calculate it manually”? You just have to add another calculated property … !?

Get-CimInstance -ClassName Win32_LogicalDisk -ComputerName ComputerName -Filter DriveType=3 | 
    Select-Object DeviceID, 
        @{'Name' = 'Size (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.size / 1GB)) } }, 
        @{'Name' = 'Freespace (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.freespace / 1GB))}},
        @{'Name' = 'Freespace (%)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate( $_.freespace / $_.Size * 100 ))}}

I meant that when there is no % column i just did - free space*100/total space.

thanks for your answer.

Percentage can also be done with string format directly:

Get-CimInstance -ClassName Win32_LogicalDisk  -Filter DriveType=3 | 
    Select-Object DeviceID, 
        @{'Name' = 'Size (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.size / 1GB)) } }, 
        @{'Name' = 'Freespace (GB)'; 'Expression' = { [string]::Format('{0:N0}', [math]::truncate($_.freespace / 1GB))}},
        @{'Name' = 'Freespace (%)'; 'Expression' = { [string]::Format('{0:P}',  $_.freespace / $_.Size )}}

The way that Olaf is presenting is better as it’s still a numeric value and this is a string. You can’t do comparison if you store it as a string