48

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lance
  • 16,092
  • 19
  • 77
  • 136
  • Up to version 3.5 of the framework, they are all backwards-compatible/inclusive with each other. So just use the subset of features that is consistent with the framework version you are targeting. – Robert Harvey Jul 27 '10 at 15:27
  • [Hey, Scripting Guy! How Do I Check Which Version of Windows PowerShell I'm Using?](https://devblogs.microsoft.com/scripting/hey-scripting-guy-how-do-i-check-which-version-of-windows-powershell-im-using/) add from a Link-only answer. – Trenton McKinney Aug 13 '21 at 23:34

6 Answers6

49

On PowerShell 2.0, just take a peek at the $PSVersionTable variable:

PS> $psversiontable

Name                           Value
----                           -----
CLRVersion                     2.0.50727.4927
BuildVersion                   6.1.7600.16385
PSVersion                      2.0
WSManStackVersion              2.0
PSCompatibleVersions           {1.0, 2.0}
SerializationVersion           1.1.0.1
PSRemotingProtocolVersion      2.1

On PowerShell 1.0, use [System.Environment]::Version:

PS> [Environment]::Version

Major  Minor  Build  Revision
-----  -----  -----  --------
2      0      50727  4927
Keith Hill
  • 194,368
  • 42
  • 353
  • 369
  • 5
    which one is the .NET version? Is it the CLRVersion? – David Klempfner Nov 26 '14 at 02:40
  • 9
    @Backwards_Dave Yes, CLRVersion indicates the version of the common language runtime. You can correlate that version number to the .NET Framework version in use - this SO answer http://stackoverflow.com/questions/212896/how-do-the-net-framework-clr-and-visual-studio-version-numbers-relate-to-each – Keith Hill Nov 26 '14 at 20:37
  • 5
    In Powershell core/ PS 7 the *old* way in this answer (`[Environment]::Version`) is the only working way to get the CLR-version (of the both mentioned here). – TNT Oct 17 '19 at 18:38
  • @KeithHill Until you can't. _The .NET Framework version number is incremented at each release, but [**the CLR version is not always incremented**](https://learn.microsoft.com/en-us/dotnet/framework/migration-guide/versions-and-dependencies#net-framework-48)_. – ᄂ ᄀ Jun 23 '23 at 07:29
18

...no, you cannot choose which .NET version you can run the script under -- George Howarth

Woah, that's not true! You can specify which version of .NET that PowerShell uses. The key is the .NET standard application configuration file, which takes the form [appname].exe.config. You can drop that in the same directory as most .NET applications -- including the PowerShell and PowerShell ISE executables -- and the CLR will automatically load any recognizable options specified within the configuration file. One of those options is the CLR version you want the application to use.

This is documented in detail in the question: How can I run PowerShell with the .NET 4 runtime?. In particular, see Emperor XLII's post.

Community
  • 1
  • 1
Joshua Honig
  • 12,925
  • 8
  • 53
  • 75
16

To get the .NET version:

[System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion

...which is, by default, the version of the CLR the assembly (System.Management.Automation.dll) compiled under.

And no, you cannot choose which .NET version you can run the script under.

George Howarth
  • 2,767
  • 20
  • 18
  • 1
    powershell.exe is hard-coded to load v2.0 of the CLR, always. This counts for both v1 and v2. – x0n Jul 30 '10 at 15:18
  • 6
    and the cleanest way for version in powershell is probably: [environment]::Version – x0n Jul 30 '10 at 15:19
  • 2
    George_Howarth, this is not true. As jmh_gr points out below you can override the version of .NET that is used (so it is NOT hard coded). I needed to check the version of a .NET4 assembly in PS1, to do this I applied the tip jmh_gr posted below (.NET 1 failed reading the newer assembly type). Running "[System.Reflection.Assembly]::GetExecutingAssembly().ImageRuntimeVersion" returns v2.0.50727, however running "[Environment]::Version" returns me the more accurate 4.0.30319.296. – Lee Campbell Feb 06 '13 at 10:33
7

The .NET version can be inferred from the version of mscorlib. So you can do the following in PowerShell to output the current version of .NET:

$a = [System.Reflection.Assembly]::Load("mscorlib")
$a.GetName().Version
driis
  • 161,458
  • 45
  • 265
  • 341
  • 4
    You don't have to load mscorlib since it's already loaded into the application domain by default: `([AppDomain]::CurrentDomain.GetAssemblies() | ? { $_.GetName().Name -eq "mscorlib" }).GetName().Version` – George Howarth Jul 27 '10 at 18:58
  • 3
    easier to just use: [environment]::Version – x0n Jul 30 '10 at 15:19
  • 2
    @x0n [enviroment]::version returnts the CLR version but mscorlib.GetName().Version returns the .net framework in use – yoel halb May 14 '14 at 17:40
  • If you want `GetName().Version` on _mscorlib_, you can also use `[Object].Assembly.GetName().Version`. In fact, in much newer PowerShell versions (not existing when the other comments were made, so they could not have known) it will go through assembly _System.Private.CoreLib_ instead, which is what you want in that case. – Jeppe Stig Nielsen Mar 09 '22 at 16:53
5

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
Jeppe Stig Nielsen
  • 60,409
  • 11
  • 110
  • 181
2

PS > [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
C:\Windows\Microsoft.NET\Framework\v2.0.50727\

Shay Levy
  • 121,444
  • 32
  • 184
  • 206