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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With