What would a PowerShell script be to return versions of the .NET Framework on a machine?
My first guess is something involving WMI. Is there something better?
It should be a one-liner to return only the latest version for each installation of .NET [on each line].
Here's the general idea:
Get child items in the .NET Framework directory that are containers whose names match the pattern v number dot number. Sort them by descending name, take the first object, and return its name property.
Here's the script:
This answer doesn't return 4.5 if that is installed. The answer below from @Jaykul and using recurse does.
There's no reliable way to do this for all platforms and architectures using a simple script. If you want to learn how to do it reliably, start at the blog post Updated sample .NET Framework detection code that does more in-depth checking.
I'm not up on my PowerShell syntax, but I think you could just call System.Runtime.InteropServices.RuntimeEnvironment.GetSystemVersion(). This will return the version as a string (something like
v2.0.50727
, I think).Not pretty. Definitely not pretty:
This may or may not work. But as far as the latest version is concerned this should be pretty reliable, as there are essentially empty folders for old versions (1.0, 1.1) but not newer ones – those only appear once the appropriate framework is installed.
Still, I suspect there must be a better way.
Gives you an instance of
Version
for the CLR the current copy of PSH is using (as documented here).