header image

Identifying the host

Posted by: | August 5, 2019 Comments Off on Identifying the host |

Identifying the host in which your PowerShell code is running could be important. For instance you might not want to run some code that takes a long time to complete in VSCode – you may prefer to ensure it runs in the console as it consumes fewer resources.

 

You can identify the host – most of the time – using $host

 

For the PowerShell console – Windows PowerShell or PowerShell Core

PS> $host.Name
ConsoleHost

 

For ISE – Windows PowerShell

PS> $host.Name
Windows PowerShell ISE Host

 

For VScode – Windows PowerShell or PowerShell Core

PS> $host.Name
Visual Studio Code Host

 

For the new Windows Terminal – Windows PowerShell or PowerShell Core

PS> $host.Name
ConsoleHost

If you need to differentiate between the traditional PowerShell console and the new Windows terminal you’ll find that the Windows terminal adds an environmental variable

WT_SESSION

which takes a value of the form – 407c1756-556e-4df2-97db-c159a616b237

under: PowerShell

Comments are closed.

Categories