Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine from a PowerShell script in runtime in which host it is running?

Tags:

powershell

I try to figure out whether my script is running in PowerShell.exe or in the ISE. If I am running in PowerShell.exe then I would like to change the size of the console window, but I don't want to impact the ISE, if I don't.

like image 607
tellingmachine Avatar asked Aug 05 '09 21:08

tellingmachine


2 Answers

You can look at the $Host variable. The name will be "ConsoleHost" in the console and "Windows Powershell ISE Host" in the ISE. Although this can probably be a little flaky to test because you're relying on user-readable strings.

Another way might be to look at $Host.UI.RawUI.BufferSize.Height which seems to be always 0 in the ISE. Something which isn't very common with a console window.

like image 183
Joey Avatar answered Oct 21 '22 14:10

Joey


$shellid also, however a better option would be to use the separate profiles for each host: Microsoft.PowerShell_Profile.ps1 and Microsoft.PowerShellISE_Profile.ps1. The respective files will run for the specific hosts. To run something in all hosts use the generic, Profile.ps1

like image 39
Chad Miller Avatar answered Oct 21 '22 14:10

Chad Miller