Velvet Star Monitor

Standout celebrity highlights with iconic style.

news

Why use "Cmd /c Powershell" instead of just "Powershell"?

Writer Sophia Terry

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

3 Answers

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

Because it gets rid of the coloring.

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

3

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.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy