Which .NET version is my PowerShell script using? Which .NET version is my PowerShell script using? powershell powershell

Which .NET version is my PowerShell script using?


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

PS> $psversiontableName                           Value----                           -----CLRVersion                     2.0.50727.4927BuildVersion                   6.1.7600.16385PSVersion                      2.0WSManStackVersion              2.0PSCompatibleVersions           {1.0, 2.0}SerializationVersion           1.1.0.1PSRemotingProtocolVersion      2.1

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

PS> [Environment]::VersionMajor  Minor  Build  Revision-----  -----  -----  --------2      0      50727  4927


...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.


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.