Search code examples
powershellpowershell-3.0

Using if condition to compare in PowerShell


I am trying to get the .Net version from the system and trying to check does my system have .Net version greater than or equal to 4.6 or not. Below is the code. From the code I am able to retrieve the release version. I am facing a problem in the if condition.

$myArray = @()

$myArray = @(Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse |
    Get-ItemProperty -Name Release,Release -EA 0 |
    Where { $_.PSChildName -match '^(?!S)\p{L}' } |
    Select Release)
Write-Output $myArray[0]
Write-Output $myArray[1]

$var1 = 393295
foreach ($var in $myArray) {
    Write-Output $var
    if ($var -ge $var1) {
        Write-Output same
    } else {
        Write-Output NOT same
    }
}

I am getting this error:

Cannot compare "@{Release=461808}" to "393295" because the objects are not the
same type or the object "@{Release=461808}" does not implement "IComparable".
At C:\Users\Desktop\DotNet.ps1:13 char:9
+     if ($var -ge $var1) {
+         ~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], ExtendedTypeSystemException
    + FullyQualifiedErrorId : PSObjectCompareTo

Solution

  • Change the foreach line as

    foreach($var in $myArray.Release)