40

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?

4
  • 8
    (dir 2>&1 *`|echo CMD);&<# rem #>echo PowerShell Commented Dec 27, 2015 at 2: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 Commented Dec 27, 2015 at 2: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. Commented Dec 27, 2015 at 5:38
  • 4
    @user2460798 Hope you does not redefine type or echo: (type 2>&1 -ea ig .|echo CMD);&<# rem #>echo PowerShell Commented Dec 27, 2015 at 8:32

2 Answers 2

35

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Sadly this trick doesn't work with PowerShell 6+, if one wanted to detect whether it's powershell.exe or pwsh.exe :(
Can't I just execute a Powershell specific command, If it's working then it's Powershell else CMD? like $PROFILE or ls
@marcus Ottoson: but works on PoweShell 7.1.5
13

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.

3 Comments

Great answer! Note 'core' isn't a thing anymore since Powershell 7. It's just 'Windows Powershell' and 'Powershell'.
@mikemaccana Interesting, and perhaps good. But it still returns "Core"...
@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.