I'm writing a new PowerShell script, and I want to make use of unicode emojies, which are now supported by the new Windows Terminal Preview. However, for a user running "legacy" PowerShell that doesn't support it, I do not wish to show the unrecognized characters, and instead I would like to show him some other text/sign.
To be more simple - I would like to know when my PS script is running in the new Terminal and show one thing and show something else for other PS terminals.
I have tried using $env:TERM_PROGRAM
. If I use is inside the vscode PS terminal it returns "vscode", but under normal PS terminal or new terminal it returns nothing.
Any ideas?
Checking your Windows version using CMD Press [Windows] key + [R] to open the “Run” dialog box. Enter cmd and click [OK] to open Windows Command Prompt. Type systeminfo in the command line and hit [Enter] to execute the command.
Invoke the command palette You can invoke most features of Windows Terminal through the command palette. The default key combination to invoke it is Ctrl + Shift + P . You can also open it using the Command palette button in the dropdown menu in Windows Terminal Preview.
From the Start MenuClick Start, type PowerShell, and then click Windows PowerShell. From the Start menu, click Start, click All Programs, click Accessories, click the Windows PowerShell folder, and then click Windows PowerShell.
Windows Terminal allows you to run Bash and other command-line utilities, including PowerShell. It also supports more characters, has a fancy new text rendering engine, and allows you to customize its appearance.
Windows Terminal is still in its infancy and not much to go by to identify it but I noticed that it adds an environment variable WT_SESSION, you might try checking for that:
if ($env:WT_SESSION) {
"I am in Windows Terminal"
} else {
"Nothing to see here..."
}
An alternative to the other answer without dependency on environment, you can check the process parent stack for the Terminal executable:
$isTerminal = {
$p = Get-CimInstance -ClassName Win32_Process -Filter ProcessID=$PID
while ($p) {
($p = Get-CimInstance -ClassName Win32_Process -Filter ProcessID=$($p.ParentProcessID) -ErrorAction Ignore)
}
}.Invoke().Name -contains 'WindowsTerminal.exe'
This is a method I've used to determine whether I'm in conemu.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With