Which .NET version is my PowerShell script using?

后端 未结 7 1216
情话喂你
情话喂你 2020-12-09 01:23

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

相关标签:
7条回答
  • 2020-12-09 01:27

    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
    
    0 讨论(0)
  • 2020-12-09 01:28

    Check out the article Hey, Scripting Guy! How Do I Check Which Version of Windows PowerShell I'm Using?. It shows where in the registry you can check to determine this.

    0 讨论(0)
  • 2020-12-09 01:28

    I've found out that you can look for that information in the directory C:\Windows\Microsoft.NET\Framework:

    cd C:\Windows\Microsoft.NET\Framework
    dir
    

    The directories inside that one will tell you the versions of the framework installed.

    Mode                LastWriteTime     Length Name
    ----                -------------     ------ ----
    d----        14/07/2009     10:48            3082
    d----        14/07/2009      4:37            v1.0.3705
    d----        14/07/2009      4:37            v1.1.4322
    d----        25/06/2010     17:26            v2.0.50727
    d----        14/07/2009     10:48            v3.0
    d----        14/07/2009     10:48            v3.5
    
    0 讨论(0)
  • 2020-12-09 01:34

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

    0 讨论(0)
  • 2020-12-09 01:35

    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
    
    0 讨论(0)
  • 2020-12-09 01:35

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

    0 讨论(0)
提交回复
热议问题