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

This entry was posted in Powershell. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s