Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 676
  • Last Modified:

How to search in an array correctly in powershell?

I have been playing with a powershell script to locate what versions of .net framework are installed.  Partially because I am trying to pick up powershell so I'd rather not just google for a script that already exists.  I'd rather figure out the best way to accomplish and understand why.

So, what I am doing right now is:
$arrVersion = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
$strNetRegPath = "hklm:\software\microsoft\net framework setup\NDP\"+$arrVersion
IF ((test-path -path $strNetRegPath) -eq $True){Write-Host "The version installed is "+ $arrVersion}

Now I know that this is wrong and I am missing things as this will obviously return all of the values in the array.  I just want it to match the correct one for each registry key in that path.  I'm not worried about the service pack level for now.  I'll get to working with the properties after I get this working.  What is the best way for me to make this work?
0
childersj
Asked:
childersj
1 Solution
 
Chris DentPowerShell DeveloperCommented:
Hello there :)

You need a loop for your array:
$arrVersion = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
ForEach ($Version in $arrVersion) {
  $strNetRegPath = "hklm:\software\microsoft\net framework setup\NDP\$Version"

  If ((Test-Path -Path $strNetRegPath) -eq $True) {
    Write-Host "The version installed is $Version"
  }
}

Open in new window

So far so good, and there's nothing wrong with that, but it's perhaps not very PowerShelly. We could play with things, for a start, we might go with a pipeline approach. There are two parts to this, the first takes our If statement and makes it Where-Object, the second deals with the results.
$Versions = "v4","v3.5","v3.0","v2.0.50727","v1.1.4322"
$Versions | 
  Where-Object { Test-Path -Path "HKLM:\software\microsoft\net framework setup\NDP\$_" } |
  Select-Object @{n='InstalledVersions';e={ $_ }}

Open in new window

The loop is still there, kind of, but now it's implicit instead of explicit.

HTH

Chris
0
 
Dale HarrisCommented:
I just ran your script, Chris.  Much enjoyed.  I'll have to keep this one for the future.  
0
 
soostibiCommented:
Or without any arrays, this solution will work for future .NET FW versions:
dir "hklm:\software\microsoft\Net Framework Setup\NDP\v*" | %{  
    if($_.name -match "Net\sFramework\sSetup\\NDP\\v(\d\.\d)"){  
        ".Net Framework Version $($matches[1]) is installed"  
    }  
}

Open in new window

0
 
childersjAuthor Commented:
Thanks for the help.  The loop is what I needed!
0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now