Author Posts

August 29, 2013 at 3:25 am

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?

August 29, 2013 at 4:12 am

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.

August 29, 2013 at 5:33 am

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

August 29, 2013 at 6:03 am

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.