Determining the PowerShell host

Tags:
The PowerShell ecosystem is more diverse than it used to be which makes determining the PowerShell host a bit more difficult.
The variable $host, and its Name property, is a good starting point.
PS> $host.Name
ConsoleHost
You get ConSoleHost if you’re running in the console.
For ISE you get – Windows PowerShell ISE Host
And for VScode you get – Visual Studio Code Host
If you want to know the PowerShell version as well it’s a bit more awkward.
$host.Version works for the console and ISE as it returns the PowerShell version. In VScode however it returns the version of VScode. The best route is to use
$PSVersionTable.PSVersion
While you’re at it you may want to check the edition of PowerShell – Core or Desktop
PS> $PSVersionTable.PSEdition
Core
To make that a bit easier
function get-powershellhost { New-Object -TypeName PSobject -Property @{ Hostname = $host.Name PSversion = $PSVersionTable.PSVersion PSEdition = $PSVersionTable.PSEdition } }
The function runs in the console, ISE and VScode and was tested across Windows PowerShell v5.1 and PowerShell v6.1
Feel free to add other information as required
 Comment on this Post