Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if I'm in powershell or cmd?

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?

like image 260
mikemaccana Avatar asked Dec 26 '15 13:12

mikemaccana


People also ask

Is PowerShell the same as CMD?

PowerShell is a more advanced version of cmd. It is not only an interface but also a scripting language that is used to carry out administrative tasks more easily. Most of the commands executed on cmd can be run on PowerShell as well.

How do I show command prompt instead of PowerShell?

You can make Shift+Right-click show Open Command window here instead of PowerShell.

Does PowerShell have all CMD commands?

Yes, kind of. Powershell sometimes use different syntax for the commands, so if you have specific commands you often use in CMD, you might want to do a quick search for those first. Most commands are the same though.

Can you open CMD from PowerShell?

The command to open Command Prompt from Windows PowerShell is exactly the same as the command to open Command Prompt from Command Prompt. In Windows PowerShell, just type start cmd.exe and press Enter.


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.

like image 107
sodawillow Avatar answered Oct 19 '22 01:10

sodawillow


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.

like image 42
not2qubit Avatar answered Oct 19 '22 03:10

not2qubit