Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect whether or not I am in powershell from a command line?

I am creating a standard windows BAT/CMD file and I want to make an IF statement to check whether or not this CMD file is run from PowerShell. How can I do that?

Edit: My underlying problem was that test.cmd "A=B" results in %1==A=B when run from CMD but %1==A and %2==B when run from PowerShell. The script itself is actually run as an old Windows Command line script in both cases, so checking for Get-ChildItem will always yield an error.

like image 915
Nilzor Avatar asked May 04 '13 09:05

Nilzor


People also ask

How do I check PowerShell commands?

Enter the name of one command, such as the name of a cmdlet, function, or CIM command. If you omit this parameter, Show-Command displays a command window that lists all of the PowerShell commands in all modules installed on the computer.

How use PowerShell command-line?

Run an old-fashioned command line (cmd.exe), type powershell and execute. Or, you can hit the PowerShell icon on the taskbar. Either way, you'll get a ready-to-use Windows PowerShell console. Use “Get-Help” cmdlet from before as a starting point for your journey.

How can I tell if PowerShell is running?

How can I tell if I am executing on PowerShell Core? Just use the $PSVersionTable object and examine the PSEdition value. If it returns 'Core' you are running on PowerShell core.

Does CMD commands work in PowerShell?

The executables can be run from any command-line shell, like PowerShell. This includes script files that may require other shells to work properly. For example, if you run a Windows batch script ( . cmd file) in PowerShell, PowerShell runs cmd.exe and passes in the batch file for execution.


2 Answers

One way, it to see what your process name is, and then check its attributes:

title=test
tasklist /v /fo csv | findstr /i "test"

As long as you use a unique name in place of Test, there should be little room for error.

You will get back something like:

"cmd.exe","15144","Console","1","3,284 K","Running","GNCID6101\Athomsfere","0:00:00","test"

When I ran the above code from a bat file, my output was:

"powershell.exe","7396","Console","1","50,972 K","Running","GNCID6101\Athomsfere","0:00:00","

like image 175
Austin T French Avatar answered Oct 20 '22 19:10

Austin T French


A potentially simpler approach that may work for you. If not, it may be suitable for others.

  1. Create 2 separate script files: test.ps1 and test.cmd
  2. Don't include extension when calling the script. I.e. call as <path>\test (or just test if folder is in the path environment variable.
  3. This works because CMD prioritises which script to execute as: .bat > .cmd, whereas Powershell prioritises: .ps1 > .bat > .cmd.

The following is the output of a CMD session:

C:\Temp>copy con test.cmd
@echo cmd^Z
        1 file(s) copied.

C:\Temp>copy con test.ps1
Write-Output "ps1"^Z
        1 file(s) copied.

C:\Temp>.\test
cmd

C:\Temp>

And calling test from Powershell:

PS C:\Temp> .\test
ps1
PS C:\Temp>
like image 22
Disillusioned Avatar answered Oct 20 '22 20:10

Disillusioned