# Question about Unit Conversion for Comparison

**URL:** <https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346>\
**Category:** PowerShell Help\
**Created:** [May 23, 2023, 11:23am UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346 "2023-05-23T11:23:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![FizzySoda](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@FizzySoda](https://forums.powershell.org/u/FizzySoda)\
**Post date:** [May 23, 2023, 11:23am UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346/1 "2023-05-23T11:23:42Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 23, 2023, 1:45pm UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346/2 "2023-05-23T13:45:36Z")

</div>

Either store the value in a variable pre Select-Object or enclose it in another $() like `$([int32]($_.FreeSpace / 1GB))`

---

<div class="post-metadata">

**Author:** ![FizzySoda](https://avatars.discourse-cdn.com/v4/letter/f/aeb1de/32.png) [@FizzySoda](https://forums.powershell.org/u/FizzySoda)\
**Post date:** [May 23, 2023, 3:16pm UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346/3 "2023-05-23T15:16:31Z")

</div>

Neither seems to be working. I might not be coding this as you would expect.

```auto
Get-CimInstance -ClassName Win32_LogicalDisk | Where-Object DeviceID -eq "C:" |
 Select-Object -Property { $([int32]($_.FreeSpace / 1GB)) }

```

```auto
$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”.

---

<div class="post-metadata">

**Author:** ![neemobeer](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/neemobeer/32/4087_2.png) [@neemobeer](https://forums.powershell.org/u/neemobeer)\
**Post date:** [May 23, 2023, 3:29pm UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346/4 "2023-05-23T15:29:13Z")

</div>

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.

```auto
$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.

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:14pm UTC](https://forums.powershell.org/t/question-about-unit-conversion-for-comparison/22346/5 "2024-05-16T20:14:52Z")

</div>


