Search code examples
powershellwindows-terminal

How to conditionally import modules in PowerShell profile based on the currently running terminal?


I want to import a few modules (posh-git, oh-my-posh, etc) only when PowerShell is launched from Windows Terminal. When PowerShell is launched using conhost or from Cmder, these imports should be excluded.

But I can see that $profile points to the same file when I open PowerShell from Windows Terminal or conhost or Cmder.

Is there a way I can identify the current terminal being used so I can do something like this in my $profile file?

If ($TERM -eq 'WT') {
    Import-Module posh-git
    Import-Module oh-my-posh
} 

Solution

  • You can use one of the automatic variables WSLENV,WT_PROFILE_ID or WT_SESSION to check if Powershell runs in Windows Terminal. They do not exist in "standalone" Powershell.

    For example:

    function Test-IsWindowsTerminal { [bool]($env:WT_Session)}