How to determine if the current host can display emoji characters programmically #12453

Closed
opened 2026-01-31 03:16:05 +00:00 by claunia · 9 comments
Owner

Originally created by @plastikfan on GitHub (Feb 7, 2021).

Is there a way to find out if the current host is Windows Terminal, Console, PowerShell ISE or any other host? I am asking this question from a PowerShell perspective (or any .Net Core api would suffice). I need to determine the characterstics of the currently running host and act accordingly. To be more specific, I have PowerShell functionality this displays emjoi's and as I am currently using Windows Terminal I have inadvertantly created functionality that will not work as I expected in the standard windows console.

In a powershell environment, we have access to a global variable $Host:

In Windows Terminal:

λ $host

Name             : ConsoleHost
Version          : 7.1.1
InstanceId       : ed19c947-594b-472c-abb5-d0d00306f1dd
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-GB
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

and in WIndows Console:

λ $host

Name             : ConsoleHost
Version          : 7.1.1
InstanceId       : ed19c947-594b-472c-abb5-d0d00306f1dd
UI               : System.Management.Automation.Internal.Host.InternalHostUserInterface
CurrentCulture   : en-GB
CurrentUICulture : en-GB
PrivateData      : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy
DebuggerEnabled  : True
IsRunspacePushed : False
Runspace         : System.Management.Automation.Runspaces.LocalRunspace

which are both exactly the same. I was hoping there would have been a property identifying it as the Windows Terminal or Console or any other such host type.

$host is of type: [System.Management.Automation.Host.PSHost.InternalHost], but looking through that documentation, I couldn't find anything that could help in this regard.

Alternatively, is there a way to determine if the host can render emojis? If the host is not capable of rendering unicode emojis then I need to take alternative action, (ie display alternative ascii characters). The current behaviour of Windows Console.

To illustrate my problem here is the result of some powershell functionality:

  • Windows terminal:

help display-emojis win-term

  • Windows Console:

help display-emojis win-cons

As you can see, in Windows Terminal, the Mandatory and PipeValue fields contain ✔️ and ✖️ , but in WIndows Console we simply see an ugly un-identofyable placeholder characters. I need to be able to take evasive action and display an alternative ascii sequence instead.

Thanks.

Originally created by @plastikfan on GitHub (Feb 7, 2021). Is there a way to find out if the current host is Windows Terminal, Console, PowerShell ISE or any other host? I am asking this question from a PowerShell perspective (or any .Net Core api would suffice). I need to determine the characterstics of the currently running host and act accordingly. To be more specific, I have PowerShell functionality this displays emjoi's and as I am currently using Windows Terminal I have inadvertantly created functionality that will not work as I expected in the standard windows console. In a powershell environment, we have access to a global variable $Host: In Windows Terminal: ``` λ $host Name : ConsoleHost Version : 7.1.1 InstanceId : ed19c947-594b-472c-abb5-d0d00306f1dd UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-GB CurrentUICulture : en-GB PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace ``` and in WIndows Console: ``` λ $host Name : ConsoleHost Version : 7.1.1 InstanceId : ed19c947-594b-472c-abb5-d0d00306f1dd UI : System.Management.Automation.Internal.Host.InternalHostUserInterface CurrentCulture : en-GB CurrentUICulture : en-GB PrivateData : Microsoft.PowerShell.ConsoleHost+ConsoleColorProxy DebuggerEnabled : True IsRunspacePushed : False Runspace : System.Management.Automation.Runspaces.LocalRunspace ``` which are both exactly the same. I was hoping there would have been a property identifying it as the Windows Terminal or Console or any other such host type. $host is of type: [System.Management.Automation.Host.PSHost.InternalHost], but looking through that documentation, I couldn't find anything that could help in this regard. Alternatively, is there a way to determine if the host can render emojis? If the host is not capable of rendering unicode emojis then I need to take alternative action, (ie display alternative ascii characters). The current behaviour of Windows Console. To illustrate my problem here is the result of some powershell functionality: * Windows terminal: ![help display-emojis win-term](https://user-images.githubusercontent.com/49785914/107159531-9028e500-6988-11eb-9019-b787bdf6659c.jpg) * Windows Console: ![help display-emojis win-cons](https://user-images.githubusercontent.com/49785914/107159535-9d45d400-6988-11eb-9b51-0369f2ef12eb.jpg) As you can see, in Windows Terminal, the Mandatory and PipeValue fields contain ✔️ and ✖️ , but in WIndows Console we simply see an ugly un-identofyable placeholder characters. I need to be able to take evasive action and display an alternative ascii sequence instead. Thanks.
claunia added the Needs-TriageNeeds-Tag-Fix labels 2026-01-31 03:16:05 +00:00
Author
Owner

@237dmitry commented on GitHub (Feb 7, 2021):

In PowerShell Core

(Get-Process -id $pid).Parent.ProcessName

In Windows Powershell via WMI Win32_Process

@237dmitry commented on GitHub (Feb 7, 2021): In PowerShell Core ``` (Get-Process -id $pid).Parent.ProcessName ``` In Windows Powershell via WMI `Win32_Process`
Author
Owner

@plastikfan commented on GitHub (Feb 7, 2021):

ok, that does work, but I don't understand why, and also I wonder what whould be returned under linux and mac. Its strange that the console returns 'cmd' when the process being run is 'pwsh'. Is this likely to be a stable property ie not like to change in future versions?

@plastikfan commented on GitHub (Feb 7, 2021): ok, that does work, but I don't understand why, and also I wonder what whould be returned under linux and mac. Its strange that the console returns 'cmd' when the process being run is 'pwsh'. Is this likely to be a stable property ie not like to change in future versions?
Author
Owner

@plastikfan commented on GitHub (Feb 7, 2021):

Unfortunately using that command is not platform independent so is not usable. Under mac, it returns the name of the shell which is 'zsh', so I need a reliable platform independant method.

@plastikfan commented on GitHub (Feb 7, 2021): Unfortunately using that command is not platform independent so is not usable. Under mac, it returns the name of the shell which is 'zsh', so I need a reliable platform independant method.
Author
Owner

@skyline75489 commented on GitHub (Feb 7, 2021):

For cross-platform support you can use $env:TERM_PROGRAM, but unfortunately Windows Terminal does not have it right now. You’ll need to use $env:WT_SESSION.

See #1040 for more discussion on this topic.

@skyline75489 commented on GitHub (Feb 7, 2021): For cross-platform support you can use $env:TERM_PROGRAM, but unfortunately Windows Terminal does not have it right now. You’ll need to use $env:WT_SESSION. See #1040 for more discussion on this topic.
Author
Owner

@237dmitry commented on GitHub (Feb 8, 2021):

Unfortunately using that command is not platform independent so is not usable.

As a rule terminals in linux have no such a problem. And checking the host may be reduced to determine what OS.

$isWindows, $isLinux, $isMacOS

A bigger problem may be the lack of a font capable of displaying emoji.

@237dmitry commented on GitHub (Feb 8, 2021): > Unfortunately using that command is not platform independent so is not usable. As a rule terminals in linux have no such a problem. And checking the host may be reduced to determine what OS. `$isWindows`, `$isLinux`, `$isMacOS` A bigger problem may be the lack of a font capable of displaying emoji.
Author
Owner

@plastikfan commented on GitHub (Feb 8, 2021):

For cross-platform support you can use $env:TERM_PROGRAM, but unfortunately Windows Terminal does not have it right now. You’ll need to use $env:WT_SESSION.

See #1040 for more discussion on this topic.

Ok well it seems like the problem I'm running into doesn't currently have a solution, so the best way forward I can see at the moment is to invoke platform specific functionality. For windows, I can check $env:WT_SESSION. For mac and Linux, I'll need to do a bit more research, perhaps emoji support is no so tricky on those platforms, or simply disable emojis altogether for the time being and hope that issue #1040 comes to the rescue at some point in the future.

I was hoping that there would have been a way to attempt to display an emoji and this mystical function would throw an exception that could be caught and handled.

@plastikfan commented on GitHub (Feb 8, 2021): > > > For cross-platform support you can use $env:TERM_PROGRAM, but unfortunately Windows Terminal does not have it right now. You’ll need to use $env:WT_SESSION. > > See #1040 for more discussion on this topic. Ok well it seems like the problem I'm running into doesn't currently have a solution, so the best way forward I can see at the moment is to invoke platform specific functionality. For windows, I can check $env:WT_SESSION. For mac and Linux, I'll need to do a bit more research, perhaps emoji support is no so tricky on those platforms, or simply disable emojis altogether for the time being and hope that issue #1040 comes to the rescue at some point in the future. I was hoping that there would have been a way to attempt to display an emoji and this mystical function would throw an exception that could be caught and handled.
Author
Owner

@DHowett commented on GitHub (Feb 8, 2021):

Since I am accumulating users who are depending on WT_SESSION for detection rather than session management, I will expedite plans to remove that environment variable.

Detecting which exact terminal you're using on Windows is going to be incorrect most of the time for the purposes of turning features on/off. There are other terminal emulators on Windows, and conhost itself is going to be able to do all of this and more eventually.

@DHowett commented on GitHub (Feb 8, 2021): Since I am accumulating users who are depending on `WT_SESSION` for detection rather than session management, I will expedite plans to remove that environment variable. Detecting which exact terminal you're using on Windows is going to be incorrect most of the time for the purposes of turning features on/off. There are other terminal emulators on Windows, and conhost itself is going to be able to do all of this and more eventually.
Author
Owner

@plastikfan commented on GitHub (Feb 11, 2021):

Hi @DHowett, can I assume that issue #1040 is the issue to track to be kept informed about the status of WT_SESSION? I was planning on using this as a short term fix, but now you're planning on removing it without a work-around.

@plastikfan commented on GitHub (Feb 11, 2021): Hi @DHowett, can I assume that issue #1040 is the issue to track to be kept informed about the status of WT_SESSION? I was planning on using this as a short term fix, but now you're planning on removing it without a work-around.
Author
Owner

@DHowett commented on GitHub (Feb 12, 2021):

1040 will be the right issue to track for determining how to detect Terminal.

@DHowett commented on GitHub (Feb 12, 2021): 1040 will be the right issue to track for determining how to detect Terminal.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#12453