Search code examples
powershellhost

How to determine if my PowerShell script is being executed by Windows Terminal?


I want to do something special when my PowerShell script is being executed by Windows Terminal and not the normal PowerShell command line or ISE. How to determine that? I checked some built-in variables like $Host and can't find related information.


Solution

  • How to known if ps1 file executed in WT?

    Looking for $env:WT_SESSION only works if checked from PS/ WT CommandLine, but always not defined yet if checked from its ps1 file (while ps1 script executed).

    I have tried. The method that works both from Commandline and PS1 file is by looking for Get-Process

    If ('WindowsTerminal' -in (Get-Process).Name) {'Running in WT'} else {'Not Running in WT'}
    Pause
    

    Using Function:

    function IsWTRun {'WindowsTerminal' -in (Get-Process).Name}
    

    The output True/ False.