#PSTip Identifying the name of the PowerShell host

Note: This tip requires PowerShell 2.0 or above.

$host automatic variable contains the details of the PowerShell host. For example, information such as the name of the host, UI culture information, etc. We can use this automatic variable to find the name of the PowerShell host.

$host.Name

The output of the above snippet will be “ConsoleHost” for PowerShell.exe and “Windows PowerShell ISE Host” for PowerShell ISE.

When in a remote session (for example, using Enter-PSSession) or referring to $host.name inside the Invoke-Command’s script block, you will receive the host name as ServerRemoteHost.

PS>Invoke-Command -ComputerName Server01 -ScriptBlock {$host.name}
ServerRemoteHost

This is especially useful when your script needs to act in a different way in a remote session as compared to a local session.

About the author: Ravikanth C

Ravikanth is the founder and editor of the PowerShell Magazine. He is also a PowerShell MVP who works at Dell Inc. He blogs at http://www.ravichaganti.com/blog and you can follow him on Twitter @ravikanth.

Related Posts

%d bloggers like this: