Identifying .NET Framework 4.5

by Jan 14, 2013

PowerShell 3.0 can run both on .NET Framework 4.0 and 4.5. .NET Framework 4.5 adds additional objects and members, so for example this line will list the members of an enumeration such as System.ConsoleColor in .NET 4.5, but not in .NET 4.0:

PS> [System.ConsoleColor].DeclaredMembers.Name     # requires .NET 4.5

Of course there are ways to work around this but that's not the point here. The point is: the version of .NET on your machine can make a difference to your code. How can PowerShell code determine the .NET version present?

Interestingly, $PSVersionTable returns a version 4.0 both for .NET 4.0 and .NET 4.5. To test for .NET 4.5, you need to look at the revision number and check whether it is greater than 17.000:

PS> ($PSVersionTable.CLRVersion.Major -eq 4 -and $PSVersionTable.CLRVersion.Revision -gt 17000)

Twitter This Tip! ReTweet this Tip!