Skip to content
Menu
The Lonely Administrator
  • PowerShell Tips & Tricks
  • Books & Training
  • Essential PowerShell Learning Resources
  • Privacy Policy
  • About Me
The Lonely Administrator

Testing for PowerShell in Windows Terminal

Posted on December 20, 2019December 20, 2019

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.

Manage and Report Active Directory, Exchange and Microsoft 365 with
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.

Getting the parent process in PowerShell

$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.

image

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.

Getting the parent process with Get-CimInstance

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.

Testing with verbose output

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.


Behind the PowerShell Pipeline

Share this:

  • Click to share on X (Opens in new window) X
  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on Mastodon (Opens in new window) Mastodon
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on Pocket (Opens in new window) Pocket
  • Click to share on Reddit (Opens in new window) Reddit
  • Click to print (Opens in new window) Print
  • Click to email a link to a friend (Opens in new window) Email

Like this:

Like Loading...

Related

5 thoughts on “Testing for PowerShell in Windows Terminal”

  1. Justin Grote says:
    December 20, 2019 at 11:26 am

    You can also just check for the WT_SESSION environment variable, much easier 🙂

    function Test-ISWindowsTerminal{
    return [bool]($env:WT_SESSION)
    }

    1. Jeffery Hicks says:
      December 20, 2019 at 11:50 am

      Thanks. I had forgotten that the Windows Terminal team added that. Probably for the very reason I wrote my function.

  2. Keith Hill says:
    December 20, 2019 at 8:44 pm

    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.

  3. Pingback: ICYMI: PowerShell Week of 28-December-2019 | PowerShell.org
  4. Pingback: PowerShell Schnipseljagd 52/2019 – Last of the decade! – PowerShell Usergroup Austria

Comments are closed.

reports

Powered by Buttondown.

Join me on Mastodon

The PowerShell Practice Primer
Learn PowerShell in a Month of Lunches Fourth edition


Get More PowerShell Books

Other Online Content

github



PluralSightAuthor

Active Directory ADSI Automation Backup Books CIM CLI conferences console Friday Fun FridayFun Function functions Get-WMIObject GitHub hashtable HTML Hyper-V Iron Scripter ISE Measure-Object module modules MrRoboto new-object objects Out-Gridview Pipeline PowerShell PowerShell ISE Profile prompt Registry Regular Expressions remoting SAPIEN ScriptBlock Scripting Techmentor Training VBScript WMI WPF Write-Host xml

©2025 The Lonely Administrator | Powered by SuperbThemes!
%d