9

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.

5
  • 1
    I don't think there is an easy way unless you would look at what process started cmd.exe and even then you won't catch all powershell runtimes. Maybe if you could explain what your goal is we could suggest another way to solve your problem. Commented May 4, 2013 at 12:24
  • You're right. See update Commented May 4, 2013 at 15:43
  • 2
    stackoverflow.com/questions/4940375/… explains on how to get around that Commented May 4, 2013 at 16:10
  • 1
    lars-truijens comment/link solves my underlying problem, but if anyone still wish to answer the original question for academic purposes, feel free :) Commented May 4, 2013 at 16:33
  • 1
    @Nilzor in the future be aware of the XY Problem. Once you told us your final goal, instead of asking about the problem to the solution you came up with for the final goal, you got an answer right way. Its a hard habit to break (at least it was for me) Commented May 4, 2013 at 17:11

3 Answers 3

5

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","

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

8 Comments

Except, as I found out, when you run a .CMD file from PowerShell it will indeed run it as an "old" command line script. title=test is then a valid command. Neither commands will produce an error in either shells
Ok so you mean if I look for "powershell" and find it, I know I am in Powershell? I guess I could do that by using grep for Windows or something unless you have a simpler solution
You mean to grep the output, yes that would work, or the built in delims. I can get you the delims line in a few, I have to do something for a bit and then will be backl
This almost works. If you start out in PowerShell.exe and call cmd.exe, but then run this, it shows powershell.exe
@ferventcoder what are you running in the cmd.exe? Something like cmd.exe C:\path\file.bat and then the above code, then yes you would be correct...?
|
2

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>

Comments

0

Couldn't you try to execute a Get-ChildItem and then check %ERRORLEVEL% to see if it returns an exe not found?

http://ss64.com/nt/errorlevel.html

2 Comments

See edit... I think I misunderstood the problem and asked the wrong question, so your solution won't work
Unfortunately Get-ChildItem doesn't work inside a .cmd file even if called from PowerShell sho ERRORLEVEL is always set.

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.