I have pretty much migrated to Windows Terminal as my primary PowerShell interface. Even though my daily session is PowerShell 7 I love that I can open up other sessions in the same application. yes, I know there are still limitations and that many of you prefer ConEmu. And that's fine. Windows Terminal suits my needs. One of the fun things I'm doing is dressing up the PowerShell 7 tab. I wrote about that here. My PowerShell 7 profile has code to use a custom prompt to rotate images. However, I realized that it only makes snese to run that code if my PowerShell 7 session is running in Windows Terminal. I needed a way to discover that piece of information.
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
In PowerShell 7, actually beginning back in PowerShell Core, you can easily get the parent process.
$PID is an automatic variable that reflects the current PowerShell process id. I should point out that this session IS running in Windows Terminal. The tricky thing is that right now I'm launching using the pwsh-preview command which is really a batch file that is launching pwsh.exe. I could probably modify the Windows Terminal profile to launch the v7 executable directly but it doesn't matter. I can check the grandparent process.
And in fact, I am running in Windows Terminal.
In Windows PowerShell, the story is a little different because there is no CodePproperty to return the parent process when using Get-Process. Fortunately, you can use Get-CimInstance to retrieve the information.
With these snippets, I built a relatively simple function which you can find as a GitHub gist.
In my Profile I can dot source the function and set a variable:
$inWindowsTerminal = Test-IsWindowsTerminal
If I have to run something that depends on WindowsTerminal, I now have a way to check.
The function won't work if you are running PowerShell in a WSL instance hosted in Windows Terminal. It also won't work if you are running a nested PowerShell instance since I'm not checking anything beyond the grandparent process. Although you could modify the code to loop through until you reach the oldest ancestor process.
I included verbose output as well.
I hope you find this useful. Enjoy and have a great weekend.
Update:
Shortly after publishing, I got a very useful comment that reminded me that the Windows Terminal team adds an environmental variable to provide GUID. This means you can create a very, very simple function like this:
Function Test-IsWindowsTerminal { [bool]($env:WT_Session)}
Or you can use my function if you want a bit more detail about the process itself.
You can also just check for the WT_SESSION environment variable, much easier 🙂
function Test-ISWindowsTerminal{
return [bool]($env:WT_SESSION)
}
Thanks. I had forgotten that the Windows Terminal team added that. Probably for the very reason I wrote my function.
You have to be a little careful using $env:WT_SESSION. If you launch VSCode from within Windows Terminal, that env var will be defined in the VSCode terminal session, which is *not* running Windows Terminal.