powershellversionpowershell-7

How to get the "Windows PowerShell" version? `pwsh --version` seems to be giving the "PowerShell 7" version


In both "PowerShell 7" and "Windows PowerShell", when I do pwsh --version, I get PowerShell 7.4.5.

However, in "Windows PowerShell", when I do Set-PSReadLineOption -PredictionSource HistoryAndPlugin [1], I get the following error

Set-PSReadLineOption : The prediction plugin source is not supported in this version of PowerShell. The 7.2 or a
higher version of PowerShell is required to use this source.
At line:1 char:1
+ Set-PSReadLineOption -PredictionSource HistoryAndPlugin
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Set-PSReadLineOption], ArgumentException
    + FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.SetPSReadLineOption

indicating that "Windows PowerShell" is not actually in version 7.4.5. So how do I get the actual version of "Windows PowerShell"?

Having two PowerShells is already confusing enough. It would appear "PowerShell 7" is leaking state onto "Windows PowerShell" to have it say it's in version 7.4.5, when it's not.

Any recommendations to avoid confusion going forward? Should I just use "PowerShell 7" exclusively?


[1] command comes from PowerShell docs on "Using predictors in PSReadLine"


Solution

  • pwsh --version

    Displays the version of this PowerShell exectuable. Additional parameters are ignored.

    returns the version of the pwsh.exe command not the version of the current CLI (command line interface). I guess this parameter is originated from the cross-platform Linux operating system, see also: How to check installed program version?

    This means that pwsh --version is indeed supposed to return its own executable version number (e.g. PowerShell 7.4.5) even if you give this command from another shell, as the good old cmd prompt. Just as the other way around -using the cmd --version command under PowerShell (or any other CLI)- would return the CMD command version:

    cmd --version
    Microsoft Windows [Version 10.0.19045.4780]
    (c) Microsoft Corporation. All rights reserved
    

    As you found out yourself, the correct way to check the current PowerShell version is using the $PSVersionTable command (see also: Determine installed PowerShell version):

    # Windows PowerShell
    $PSVersionTable
    
    Name                           Value
    ----                           -----
    PSVersion                      5.1.19041.4780
    PSEdition                      Desktop
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0...}
    BuildVersion                   10.0.19041.4780
    CLRVersion                     4.0.30319.42000
    WSManStackVersion              3.0
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    

    This represents the current PowerShell version that corresponds to the same version of the executable (pwsh.exe) that has been initially launched (possibly through a shortcut).
    This will also show in the following command that opens a temporary (PowerShell 7.4.5) shell and returns the results the current command line independent of the current CLI (version):

    pwsh -Command '$PSVersionTable'
    
    Name                           Value
    ----                           -----
    PSVersion                      7.4.5
    PSEdition                      Core
    GitCommitId                    7.4.5
    OS                             Microsoft Windows 10.0.19045
    Platform                       Win32NT
    PSCompatibleVersions           {1.0, 2.0, 3.0, 4.0…}
    PSRemotingProtocolVersion      2.3
    SerializationVersion           1.1.0.1
    WSManStackVersion              3.0