Q:

How to compare .net FW version number with number stored in variable

Super new here and to #Powershell. I'm making a script to check whether the .Net Framework version that is installed is greater than or equal to a version number stored in a variable.
The issue I have is when setting up the variable that filters down to the version number.
$installed = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' |  Get-ItemPropertyValue -Name Version | Where { $_.Version -ge $software }) -ne $null
I want to compare the .Net Framework Version found in

HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full

to whichever version is installed on a Windows 10 computer to see if it is greater than or equal. I've tried comparing the release number in the registry, but the Version is more relevant for what I'm doing.
I want to write a message to the console and to a text file
$software = '4.7.02053'
$installed = (Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full\' | Get-ItemPropertyValue -Name Version | Where { $_.Version -ge $software }) -ne $null
If(-not $installed) {
    "$software is NOT installed."| Out-File -FilePath C:\Pre-Req_Log.txt -append
    Write-Host "'$software' is NOT installed.";
    pause
} else {
    "$software is installed."| Out-File -FilePath C:\Pre-Req_Log.txt -append
    Write-Host ".Net FW '$software' is installed."
    }
My expected result is to see '4.7.02053' is (or not) Installed in the text file and it be correct. It doesn't matter if it's equal, as long as it's that version or greater I will be happy.

A:

To compare version numbers, don't compare them as strings, cast them to [version] (System.Version) and then compare them:
$refVersion = [version] '4.7.02053'

$installedVersion = [version] (Get-ItemPropertyValue -LiteralPath 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Version)

if ($installedVersion -ge $refVersion) {
  # installed
  # ...
}
else {
  # not installed
  # ...
}

If you use these [version] instances inside an expandable string ("..."), you'll get the expected string representation, but note that outputting them as-is to the console or via Out-File / > will show a tabular display with the version-number components shown individually. To force the usual string representation, use enclosing "..." - e.g., "$refVersion", or call .ToString(), e.g., $refVersion.ToString()

