I've been playing with OpenSSH on Windows and it looks like the normal Unix aliases are missing. I'm not sure whether it's starting powershell or cmd when I log in to a Windows machine via SSH. What's the correct way to see the currently running shell on Windows?
-
7``(dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell`` – user4003407 Dec 27 '15 at 02:34
-
Curious what the best answer could be. I would think this one could be tough looking for a "clean" way to do it. I mean while you are at it you could also ask to see if you are running in ruby or python as well. PowerShell and cmd are not really at all comparable. PowerShell is .net based and its similarities to cmd are there to ease the transition for people that used to use cmd batch – Matt Dec 27 '15 at 02:44
-
@PetSerAl This doesn't work if the profile redefines `dir` to invoke cmd.exe's dir command (which my profile does). But it's pretty clever nonetheless. – Χpẘ Dec 27 '15 at 05:38
-
4@user2460798 Hope you does not redefine `type` or `echo`: `(type 2>&1 -ea ig .|echo CMD);&<# rem #>echo PowerShell` – user4003407 Dec 27 '15 at 08:32
2 Answers
All credit goes to PetSerAl, this had to be posted as an aswer:
(dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell
Within Win32-OpenSSH
this command also works, and outputs CMD
.
NB : Win32-OpenSSH
seems a bit limited, cd
is not recognized on my system.

- 1
- 1

- 12,497
- 4
- 34
- 44
-
7Sadly this trick doesn't work with PowerShell 6+, if one wanted to detect whether it's powershell.exe or pwsh.exe :( – Marcus Ottosson Jul 16 '19 at 19:00
-
Can't I just execute a Powershell specific command, If it's working then it's Powershell else CMD? like `$PROFILE` or `ls` – Rajan Apr 21 '21 at 12:26
-
I'd like to expand on @sodawillow's answer to also distinguish between using Powershell (powershell.exe) known as Desktop
and PWSH (pwsh.exe) known as Core
.
(dir 2>&1 *`|echo CMD);&<# rem #>echo ($PSVersionTable).PSEdition
# Returns one of: CMD, Core, Desktop
This works in all instances where a sub-shell is not instantiated. What that means is that it does not work from opening a default sub-process in Python, as it always uses CMD when interacting with windows. This is actually set by the Windows environment variable: ComSpec
always pointing to C:\Windows\system32\cmd.exe
.
For example:
(Starting the python interpreter from a pwsh shell.)
>>> import os, subprocess
>>> c="(dir 2>&1 *`|echo CMD);&<# rem #>echo($PSVersionTable).PSEdition"
>>> subprocess.call(c,shell=True)
CMD
For other Python shell detection schemes, please see this good post.
UPDATE: 2020-05-01
I managed to get the above working, but with the obnoxious side effect of always loading the powershell profile, before executing. The trick was to specify execute=<path-to-powershell-exe>
like this:
(Start a python CLI.)
import os, subprocess
c="(dir 2>&1 *`|echo CMD);&<# rem #>echo($PSVersionTable).PSEdition"
e="C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"
subprocess.call(c, shell=True, executable=e)
# output:
# <blah blah from profile>
# Desktop
# 0
I have not been able to circumvent the powershell profile issue. But apparently it is something being worked on. See here and here.

- 14,531
- 8
- 95
- 135
-
1Great answer! Note 'core' isn't a thing anymore since Powershell 7. It's just 'Windows Powershell' and 'Powershell'. – mikemaccana Apr 28 '20 at 10:24
-
1@mikemaccana Interesting, and perhaps good. But it still returns "Core"... – not2qubit Apr 29 '20 at 18:24
-
@mikemaccana I managed to get a step further, but I'm not able to avoid loading the *profile*. There is also a mysterious `0` at the very end of the output. – not2qubit May 01 '20 at 20:06