Hey All,
I’m trying to return the FreeSpace of a particular drive in GB for comparison. Basically, I"m just doing a check to see what the FreeSpace is prior to executing a script.
This returns a object type of system.double
(Get-CimInstance Win32_Volume -Filter “DriveLetter = ‘C:’”).FreeSpace / 1GB
This returns an object type of Selected.Microsoft.Management.Infrastructure.CimInstance
Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object DeviceID -eq “C:” |
Select-Object -Property { [int32]($_.FreeSpace / 1GB)
I can do a comparison using the -gt operator on the first one, but not the second – This makes sense to me given the types returned. I would like to know why the second statement doesn’t return a type of int32. I am setting the FreeSpace property to a int32.
Either store the value in a variable pre Select-Object or enclose it in another $() like $([int32]($_.FreeSpace / 1GB))
Neither seems to be working. I might not be coding this as you would expect.
Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object DeviceID -eq "C:" |
Select-Object -Property { $([int32]($_.FreeSpace / 1GB)) }
$FreeSpace = Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object DeviceID -eq "C:"
$Margin = $FreeSpace | Select-Object -Property { $([int32]($_.FreeSpace / 1GB)) }
IF ($Margin -gt 8 ) {Write-Output "FreeSpace is greater than 8"}
i’m still getting an error :
ExtendedTypeSystemException: Cannot compare “@{ $([int32]($.FreeSpace / 1GB)) =9}" to “8” because the objects are not the same type or the object "@{ $([int32]($.FreeSpace / 1GB)) =9}” does not implement “IComparable”.
Select and Format-* are final as in output formatting. You want to do them last or like I said store the calculation not in a select statement.
$MyDisk = Get-CimInstance -ClassName Win32_LogicalDisk -Filter "DeviceID eq 'C:'"
$FreeSpaceinGB = $MyDisk.FreeSpace / 1GB
Now it’s stored in the correct type and you can do your if logic on it.
1 Like