30

I've added my first context menu using the registry as per instructions given in this question (yay me).

I originally used this as the command that it executes, i.e. the value of the "command" key:

Cmd /C Powershell  "imageSeqView --% \"%1\""

ImageSeqView is the name of my powershell function, I import it in my powershell profile. It works fine, but I'm wondering: why use cmd to open Powershell to execute the function? Wouldn't it be simpler to just do

Powershell  "imageSeqView --% \"%1\""

That command seems to work just fine, but since it seems canonical to use cmd I'm wondering is it secretly killing puppies or something?

enter image description here

6
  • 2
    None of the answers in the linked question use cmd /c... cmd /k is rather different in that it leaves the window open after the command completes. Presumably the asker did it that way so they could see the output for debugging purposes.
    – Bob
    Commented Sep 1, 2017 at 2:43
  • 1
    It's not so much the modifier I'm wondering about, ti's the use of cmd at all, which seems to be ubiquitous from my research.
    – stib
    Commented Sep 1, 2017 at 2:45
  • Can you provide a reference that actually recommends cmd /c powershell specifically? Again, the one you linked makes no such recommendation.
    – Bob
    Commented Sep 1, 2017 at 2:45
  • the accepted answer here: social.technet.microsoft.com/Forums/scriptcenter/en-US/…
    – stib
    Commented Sep 1, 2017 at 2:47
  • 3
    At a guess, it's probably mostly cargo culting. There's a few cases where this might be desired (namely, when you want to use cmd constructs like piping), but they don't really apply when you can do the same in powershell anyway. There is nothing in MSDN documentation that suggests this is necessary, though the docs surrounding file associations are rather sparse.
    – Bob
    Commented Sep 1, 2017 at 3:21

3 Answers 3

32

There's no good reason to do this. In fact, the only real effect that happens is to slow things down.

People might think there is a good reason to do this. Using CMD has the following effects which can commonly be good in some cases:

  • Enables internal commands, like "DIR"
  • Sets environment variables, such as the PATH variable

However, in this case, neither of those benefits are gained. Let's look at both of these scenarios:

So, there can, in some cases, be a time when using "CMD /C" is useful. For example, if I use the external command PSEXEC (downloaded from SysInternals), and try to run "DIR" on a remote computer, then Windows will try to run the "DIR" command. Windows will fail to run that command since there is no "DIR.EXE", "DIR.BAT", or "DIR" file ending with another supported extension. (Supported extensions can be seen by running "ECHO %PATHEXT%".)

However, in this scenario, if I try to run "CMD /C DIR", then that will work, because Windows will look for an executable named "CMD", and will find that, and then CMD will end up successfully running the "DIR" command which is an internal part of the "CMD" command.

In this case, you can just run powershell just as easily as "CMD /C powershell", so you gain no benefit from the unnecessary "CMD /C". The only benefit I'm seeing to going through the extra step of typing "CMD /C" is to provide an example which will be useful if somebody decides to try modifying an example to run a command line "DIR" or "COPY". Having a more flexible example may be useful for some people. It's really not needed when people know what they're doing.

As for the second bullet point I provided, which is to set environment variables, that's also something that you're not actively doing in this particular case. Maybe some people think that they are helping matters by causing the PATH environment variable to be set. However, when you run commands directly (e.g., from the "Run" menu option of the Start menu), the Windows operating system may look for commands in some additional places. For instance, in Windows XP / newer you can run:

reg query "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\App Paths"

If the command you want to run is listed under "App Paths", Windows may find the program even if it isn't in the path. So, Windows is likely to find even MORE than just what CMD would find in the PATH that CMD uses.

One possible benefit is if you wanted CMD to be run so that you could refer to an environment variable like %USERPROFILE% or %LOGONSERVER% or %TEMP%/%TMP%, but since you're not doing that, you're not needing to run "CMD /C".

So, for your particular case: There's no good reason to do it. The effects you are achieving are having your computer do more work, slowing the process down, and using up more memory (all of which you are doing by negligible amounts on modern equipment).

5
  • I'm in full agreement with the advice Bob was giving. Bob was leading you down a right path. As I read things, though, I thought some more information might help provide a bit more clarity quickly.
    – TOOGAM
    Commented Sep 1, 2017 at 4:38
  • 3
    ftr - starting a process with a different priority is a good use case for cmd /c. Something like cmd /c start /low Notepad.exe. Commented Sep 1, 2017 at 5:28
  • Good answer. One thing I would've mentioned is this also runs cmd's autorun, but that's usually not set, and even when it is set it's probably not desired in a file association. And Lieven makes a good point with the priority, though again I think that's a bit odd for a file association.
    – Bob
    Commented Sep 1, 2017 at 6:59
  • 1
    cmd itself does not set environment variables. The environment is inherited from the parent process, adding cmd in between doesn't change anything about that. App Paths is only used for ShellExecute, as far as I know, not for CreateProcess, so in most circumstances (especially in shells) those won't apply (you can try it yourself, pbrush is an App Path for mspaint and won't work in many places).
    – Joey
    Commented Sep 1, 2017 at 8:23
  • @Joey perhaps more correct to say it expands the variables... but, come to think of it, if you use REG_EXPAND_SZ you can expand environment variables (using the same %syntax%) without invoking cmd. The verbs used in file associations are invoked via ShellExecuteEx, so App Paths do apply here.
    – Bob
    Commented Sep 1, 2017 at 8:45
18

Because it gets rid of the coloring.

It may be that they think people find the blue background distracting.

3
  • 1
    Don't have Windows handy, but can't you just set background color in the console properties?
    – Ruslan
    Commented Sep 1, 2017 at 12:36
  • 1
    Upvoting, despite being a competing answer to mine which suggests there isn't a reason. Because, this is a sensible reason. Good job. Of course, some people may find the black box is more distracting than the blue box. However, that is a matter of preference and people can have different preferences, so +1 for a good and accurate answer. @Rusland: Yeah, but can you manage to get that change made before a quick program disappears? (And, do you know whether changes made to one session will affect other sessions. Will settings differ between general cases & specific ones like desktop icons?)
    – TOOGAM
    Commented Sep 1, 2017 at 13:18
  • I never notice it because I use conEmu as my default console, meaning I always get my nicely configured console window (with ascii art and custom prompts and gifs of burning torches. Loads in under 10 minutes too!)
    – stib
    Commented Sep 3, 2017 at 8:53
1

With cmd powershell, you ask the current shell, explorer, to invoke cmd with parameters powershell, "imageSeqView ..." with parsed value of %1 to cmd.

In this case, "powershell" is expected by cmd to be either a cmd command, an exe or one of cmd's supported scripts, e.g. bat.

With powershell "imageSeqView ...", you ask the current shell, explorer, to invoke powershell with parameters imageSeqView ... with parsed value of %1 to powershell.

In this case, "imageSeqView" is expected by powershell to be either a cmdlet, an exe or a powershell script.

Provided that "imageSeqView" is a powershell function, the 1st way is completely unnecessary and slightly lowers the performance.

For command window options, there are similar options in powershell such as -NoExit, which should be the same as /K in cmd.

For pipe-lining, initializing env vars, powershell can do equally.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .