Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which .NET version is my PowerShell script using?

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?

like image 545
lance Avatar asked Jul 27 '10 14:07

lance


People also ask

How can I tell what version of .NET is PowerShell using?

Open Start. Search for PowerShell, right-click the top result, and select the Run as administrator option. Type the following command to check the version of . NET installed and press Enter: Get-ChildItem 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP' -Recurse | Get-ItemProperty -Name version -EA 0 | Where { $_.


2 Answers

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 
like image 106
Keith Hill Avatar answered Oct 19 '22 13:10

Keith Hill


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

like image 34
Joshua Honig Avatar answered Oct 19 '22 13:10

Joshua Honig