Solved

How to search in an array correctly in powershell?

Posted on 2011-03-14
4
657 Views
Last Modified: 2012-05-11
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
Comment
Question by:childersj
4 Comments
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 35132422
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
 
LVL 16

Expert Comment

by:Dale Harris
ID: 35132511
I just ran your script, Chris.  Much enjoyed.  I'll have to keep this one for the future.  
0
 
LVL 13

Expert Comment

by:soostibi
ID: 35133316
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
 

Author Closing Comment

by:childersj
ID: 35138287
Thanks for the help.  The loop is what I needed!
0

Featured Post

Promote certifications in your email signature

Has your company recently won an award or achieved a certification? They'll no doubt want to show it off. Email signature images used to promote certifications & awards can instantly establish credibility with a recipient and provide you with numerous benefits.

Join & Write a Comment

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The recent Microsoft changes on update philosophy for Windows pre-10 and their impact on existing WSUS implementations.
This tutorial will walk an individual through the steps necessary to install and configure the Windows Server Backup Utility. Directly connect an external storage device such as a USB drive, or CD\DVD burner: If the device is a USB drive, ensure i…
The viewer will learn how to dynamically set the form action using jQuery.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now