Checking Host

by Feb 21, 2017

In the past, Microsoft shipped two PowerShell hosts: the basic PowerShell console, and the more sophisticated PowerShell ISE. Some users used code like below to find out whether a script runs in the console or the PowerShell ISE:

$inISE = $psISE -ne $null

"Running in ISE: $inISE"

However, there are many more hosts around these days. Visual Studio can host PowerShell, and so does Visual Studio Code. And there are additional commercial editors. So if you must know whether a script runs in a given environment, use the host identifier instead:

$name = $host.Name
$inISE = $name -eq 'Windows PowerShell ISE Host'

"Running in ISE: $inISE"

Each host emits its own host name, so this approach can be adjusted to any host. When you run a script inside Visual Studio Code, for example, the host name is “Visual Studio Code Host”.

Twitter This Tip! ReTweet this Tip!