PowerTip: Find Name of PowerShell Host


Summary: Learn to find the name of the Windows PowerShell host.

Hey, Scripting Guy! Question How can I determine if I am running a Windows PowerShell script in the Windows PowerShell console?

Hey, Scripting Guy! Answer Use the Get-Host cmdlet. Here is how to check:

  if((Get-Host).name -ne "consolehost") {"Take appropriate action here"}

Comments (6)

  1. Simple but helpful. Thanks

  2. Anonymous says:

    @maekee

    Here are 4 possible values for the name – property:

    PS-Environment: => $host.name
    ————————————————————
    PowerShell Console => ConsoleHost
    PowerShell ISE => Windows PowerShell ISE Host
    Sapien PrimalScript => PrimalScriptHostImplementation
    idera POWERSHELLPLUS => PowerShellPlus Host

  3. Keith Garner says:

    Please ensure that your scripts work in all hosting environments.
    No assumptions. :^)
    Console host and the PowerShell ISE are great places to start!

  4. Boe Prox says:

    @$($User.Name)
    They are the same.

    (Get-host).GetType().Fullname -eq ($host).GetType().Fullname

  5. Maekee says:

    What other names can i get back (if not Consolehost?)

  6. $($User.Name) says:

    What is is the difference between the Get-Host cmdlet and the PowerShell automatic variable $Host?
    To determine if in the console or the ISE, check ($PSISE -EQ $Null)

Skip to main content