Search code examples
.netpowershellversion

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?


Solution

  • 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