Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to search in an array correctly in powershell?

Posted on 2011-03-14
4
662 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A safe way to clean winsxs folder from your windows server 2008 R2 editions
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

839 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