F. Jozsef F. Jozsef - 3 months ago 13
PowerShell Question

Get .net version with powershell and update

I am new to powershell, and I would like to check if .net 3.5 and 4.5 installed or not. If both installed, there is nothing to do, but if one or both of them are missing I want powershell to install it.
Getting .net installed versions:

ls -r "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP" | %{ gp $_.PSPath } | Where-Object Version | Select-Object @{Name="dNET"; Expression = {($_.PSParentPath -split "\\" | Select-Object -Last 1).Padright(4).Substring(0,4).Replace(" ","")}},Version,PSPath | Where-Object dNET -like 'v*' | ft *


Can anyone help me with the if statement? I really got lost there.

Answer

Remove the ft * statement and assign the output to a variable, then use Where-Object to determine if either version is installed:

$installedVersions = ls -r "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP" | %{ gp $_.PSPath } | Where-Object Version | Select-Object @{Name="dNET"; Expression = {($_.PSParentPath -split "\\" | Select-Object -Last 1).Padright(4).Substring(0,4).Replace(" ","")}},Version,PSPath | Where-Object dNET -like 'v*'
if(-not($installedVersions |Where-Object dNET -eq 'v3.5')){
    # install v3.5
}
if(-not($installedVersions |Where-Object dNET -eq 'v4.5')){
    # install v4.5
}
Comments