'calculate free disk space percentage powershell

I am trying to get the free percentage like present in Disk Management

$Diskmgmt = Get-Volume | select DriveLetter,FileSystemLabel,FileSystem,DriveType,HealthStatus,OperationalStatus,SizeRemaining,Size

foreach($dsk in $Diskmgmt)
{

    $dl = $dsk.DriveLetter
    $fsl = $dsk.FileSystemLabel
    $fs = $dsk.FileSystem
    $dt = $dsk.DriveType
    $hs = $dsk.HealthStatus
    $os = $dsk.OperationalStatus
    $sizer = [math]::round($dsk.SizeRemaining /1Gb, 2)
    $siz = [math]::round($dsk.Size /1Gb, 2)
    $PercentFree =  [Math]::Round(($sizer / $siz) * 100, 2)

but the calculation coming like below

Capacity     Free Space %Free
154.82 GB     200 GB     77 % 
0 GB          0 GB       77 % 
1.96 GB       6 GB       33 % 
0.15 GB       0.49 GB    31 % 
52.32 GB      99.51 GB   53 % 
11.19 GB      11.23 GB   100 % 
9.95 GB       10 GB      99 % 

Please let me know if I am doing it correctly.



Solution 1:[1]

Using get-psdrive and the 'p' format specifier:

get-psdrive c | % { $_.free/($_.used + $_.free) } | % tostring p

9.24%

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 js2010