Get-Host

This topic contains 3 replies, has 3 voices, and was last updated by Profile photo of Thomas Franke Thomas Franke 3 years, 10 months ago.

  • Author
    Posts
  • #9824
    Profile photo of Thomas Franke
    Thomas Franke
    Participant

    Determining the powershell version can be important. Often this is done by using the Get-Host cmdlet. I read somewhere that this command returns the version of the powershell host, not the version of powershell itself.

    Question: The powershell and the powershell host usually have the same version. Is there any case when the versions differ from each other?

  • #9825
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you're talking about the Console and ISE, the versions will probably always match the PowerShell version. Developers can write their own custom hosts, though, which would have their own version scheme.

  • #9829
    Profile photo of Richard Siddaway
    Richard Siddaway
    Moderator

    In PowerShell 2.0 and above you can also use the $psversiontable variable which will give some additional interesting info such as wsman version and CLR version

    $psversiontable will give the powershell version rather than the host version

  • #9833
    Profile photo of Thomas Franke
    Thomas Franke
    Participant

    Thank you!

    So Get-Host will usually return the right value, but might be wrong in some Special cases.
    $psversiontable will fail on Powershell V1 but I found a great Workaround:


    # If the $PSVersionTable variable doesn't exist, then you are running V1.
    # If it does exist, then the version will be available as $PSVersionTable.PSVersion.

    function Get-PSVersion {
    if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
    }

    Source: http://powershell.com/cs/media/p/2617.aspx

    In the end I decided to avoid getting and processing the version info and inserted a "#Requires -version 3.0" statement instead.

You must be logged in to reply to this topic.