Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In PowerShell, how do I get all registry keys with a certain property?

I want to get the installation directories of all Visual Studio versions installed on a machine (to copy a file into the Xml/Schemas directory)

The best way is to read from the registry. At \HKLM\Software\Wow6432Node\Microsoft\Visual Studio, all the versions are listed by version number: 10.0, 11.0, 12.0. However, 9.0 and 8.0 and 7.1 are still there, even though those aren't installed. So I can't just grab them all. What I need is to filter on all keys that have the InstallDir property.

I know I have to combine Get-ChildItem and Get-ItemProperty but I'm not sure how.

How do I write the Powershell command for what I'm asking?

like image 916
ageektrapped Avatar asked Mar 23 '23 11:03

ageektrapped


1 Answers

This will generate errors on the keys that don't have the property - but you can safely ignore those.

cd HKLM:\SOFTWARE\Wow6432Node\Microsoft\VisualStudio
$paths = dir | gp -Name InstallDir | select InstallDir

Looking at the output of the $paths variable on my computer where I have studio 2010 and 2012 installed:

InstallDir
----------
C:\Program Files (x86)\Microsoft Visual Studio 10.0\Common7\IDE\
C:\Program Files (x86)\Microsoft Visual Studio 11.0\Common7\IDE\

The magic is that Get-ItemProperty (gp) will accept the default parameter from the pipeline so that you can chain the output from Get-ChildItem (dir) to automatically check each of the sub-keys. The Select-Object (select) cmdlet simply eliminates the PS* properties and gives you just the raw path:

like image 97
Goyuix Avatar answered Mar 25 '23 07:03

Goyuix