6

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.

3 Answers 3

8

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.

Sign up to request clarification or add additional context in comments.

Comments

3

$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

2 Comments

$ShellId yields "Microsoft.Powershell" in both the console and the ISE, at least here.
Hmm, I'm used to dealing with sqlps which is the SQL 2008 host. The shellid between those hosts is different The use of separate profiles is still a possible solution.
1

The easiest way that I accomplish this is running:

$PROFILE.CurrentUserCurrentHost

This will make it possible to easily discern whether the current host is PowerShell Core, Windows PowerShell, ISE, or VS Code. Here are the possible values this will return:

Host Value
Core $env:USERPROFILE\Documents\PowerShell\Microsoft.PowerShell_profile.ps1
Windows $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
ISE $env:USERPROFILE\Documents\WindowsPowerShell\Microsoft.PowerShellISE_profile.ps1

I do not currently have VS Code installed, but I can confirm from when I previously did use the editor that this will return a value that follows a similar pattern (path will contain "VSCode").

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.