Which .NET version is my PowerShell script using?

Viewed 32469

I'd like to use .NET in some PowerShell scripts I'm about to write -- how do I know/declare which version of .NET I'm dealing with when these scripts run?

And is it possible to choose against which version of .NET my script will run?

6 Answers

This is an old thread, and the answer I am going to post now will not work for .NET versions from from before circa 2017.

There is a new FrameworkDescription property.

Try:

[System.Runtime.InteropServices.RuntimeInformation]::FrameworkDescription

Note, however, on some versions of Windows PowerShell (not sure about newer PowerShell 6 and 7 etc.) there are two different types System.Runtime.InteropServices.RuntimeInformation in different assemblies! And only one of them has the property. So you must qualify:

[System.Runtime.InteropServices.RuntimeInformation, Microsoft.Powershell.PSReadline]::FrameworkDescription   # does not exist
[System.Runtime.InteropServices.RuntimeInformation, mscorlib]::FrameworkDescription   # good
Related