Solved

How to search in an array correctly in powershell?

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
A brief introduction to what I consider to be the best editor for PowerShell.
The viewer will learn how to dynamically set the form action using jQuery.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

777 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