By Philip Kirkbride


2012-03-16 13:48:44 8 Comments

Is it possible to create a desktop shortcut that, when pressed, will open command prompt and run a pre-defined command?

10 comments

@Robin Hood 2014-09-12 21:39:16

Create A Shortcut That Opens The Command Prompt & Runs A Command:

Yes! You can create a shortcut to cmd.exe with a command specified after it. Alternatively you could create a batch script, if your goal is just to have a clickable way to run commands.

Steps:

  1. Right click on some empty space in Explorer, and in the context menu go to "New/Shortcut".

  2. When prompted to enter a location put either:

"C:\Windows\System32\cmd.exe /k your-command" This will run the command and keep (/k) the command prompt open after.

or

"C:\Windows\System32\cmd.exe /c your-command" This will run the command and the close (/c) the command prompt.

Notes:

  • Tested, and working on Windows 8 - Core X86-64 September 12 2014

  • If you want to have more than one command, place an "&" symbol in between them. For example: "C:\Windows\System32\cmd.exe /k command1 & command2".

@Patrick D'Souza 2015-08-19 11:21:01

This should be marked as the answer as it does not require the creation of a bat file.

@cambunctious 2015-11-03 22:45:07

you can type cmd instead of the entire path

@Dimitri Dewaele 2016-02-05 07:27:54

Addition: replace 'Example' by a bat-file. C:/workspace/startup.bat to load a bat file which prepares your command window. Hint: I always add color to the different command-shortcuts that I startup. Easy to see where you are working. Add them in the shortcut properties (right-click).

@Joe Cotton 2016-08-11 20:08:01

I use this method to open a command prompt then run a "setup" batch command file to setup parameters for future work.

@Pacerier 2017-07-29 15:23:56

@PatrickD'Souza, But you have a shortcut file.

@Alex K. 2012-03-16 13:53:18

Yes, make the shortcut's path

%comspec% /k <command>

where

  • %comspec% is the environment variable for cmd.exe's full path, equivalent to C:\Windows\System32\cmd.exe on most (if not all) Windows installs
  • /k keeps the window open after the command has run, this may be replaced with /c if you want the window to close once the command is finished running
  • <command> is the command you wish to run

@Bjorn 2014-10-19 17:32:49

Doesn't work for me.

@Alex K. 2014-10-19 17:34:50

Then something is wrong with whatever you are attempting as this is the canonical way to do it.

@Bjorn 2014-10-19 19:14:39

Coming as a non-windows user I have no idea what /1234 means, what /k means or what should go into the other boxes. I placed a command in a shortcut config window, a command that 100% works when I run it inside a cmd terminal, and it did not work in the shortcut window when I replaced your "c:\foo\bar.exe" with my command. Perhaps there's a problem with arguments, or permissions, I don't know, it just didn't work. The result is an empty terminal box with an access error.

@Alex K. 2014-10-20 10:06:32

/1234 is just an example showing where the command line goes, %comspec% /? explains /k

@Nitish Andola 2017-03-01 09:08:26

You can also create a shortcut on desktop that can run a specific command or even a batch file by just typing the command in "Type the Location of Item" bar in create shortcut wizard

  1. Right click on Desktop.
  2. Enter the command in "Type the Location of Item" bar.
  3. Double click the shortcut to run the command.

Found detailed Instructions here

@Davidson Lima 2018-05-29 20:02:29

It works perfectly.

@Ī©megaMan 2017-03-02 18:22:53

Using the Drag and Drop method

  1. From the windows search bar type in cmd to pull up the windows bar operation.
  2. When the command line option is shown, right click it and select Open File Location.
  3. The file explorer opens and the shortcut link is highlighted in the folder. If it is not highlighted, then select it.
  4. Hold down the Control key and using the mouse drag the shortcut to the desktop. If you don't see Copy to Desktop while dragging and before dropping, then push down and hold the Control key until you see the message.
  5. Drop the link on the desktop.
  6. Change properties as needed.

@Philip Kirkbride 2012-03-16 15:28:08

The solutions turned out to be very simple.

  1. Open text edit

  2. Write the command, save as .bat.

  3. Double click the file created and the command automatically starts running in command-prompt.

enter image description here

@Huai 2013-04-05 02:26:59

I would like to add to PhilipK's answer. His approach is correct. But you need to save the .bat file to the right place. For example, i tried to create a shortcut to run "outlook /safe". I saved the .bat file to the desktop. It didn't work. It however worked after I saved the file to the location where "outlook.exe" was. This shows the location of the file matters.

@Philip Kirkbride 2013-04-05 02:50:43

If you really wanted to have it run from the desktop couldn't you add 'cd Applications'(or cd what ever directory you need to be in) to the beginning of the script? Or better yet check if location is Applications Folder and if not then change directory to the proper one. You make a good point though, anyone doing this should consider.

@Tqn 2014-07-23 05:09:23

You could just put in the batch file: @echo off cd "%HOMEDRIVE%/Your/Path/To/program.exe" How is this the accepted answer, though?

@Philip Kirkbride 2016-05-19 02:44:26

This was one of the first answers and still the simplest for across all windows for me (feels more like linux). I've up voted a lot of the answers too very good information.

@Alias 2017-05-28 08:13:39

This helped me. Many many thanks

@bytedev 2017-06-12 16:36:31

But the question was how to do this in a "shortcut"....this is a batch file..

@Philip Kirkbride 2017-06-12 16:37:44

@nashwan I interpret "shortcut" is a clickable icon on the desktop.

@bytedev 2017-06-13 16:45:52

@PhilipKirkbride I can see that :-)

@Adiii 2016-10-31 06:00:28

  1. first goto that folder from where you to what to open command prompt where its desktop or some other location
  2. make a text file in that location just write cmd -c and save name.bat
  3. double click so your CMD path will be of that folder enter image description here

@joef 2014-05-21 13:16:38

This is an old post but I have issues with coming across posts that have some incorrect information/syntax...

If you wanted to do this with a shorcut icon you could just create a shortcut on your desktop for the cmd.exe application. Then append a /K {your command} to the shorcut path.

So a default shorcut target path may look like "%windir%\system32\cmd.exe", just change it to %windir%\system32\cmd.exe /k {commands}

example: %windir%\system32\cmd.exe /k powercfg -lastwake

In this case i would use /k (keep open) to display results.

Arlen was right about the /k (keep open) and /c (close)

You can open a command prompt and type "cmd /?" to see your options.

http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/cmd.mspx?mfr=true

A batch file is kind of overkill for a single command prompt command...

Hope this helps someone else

@Philip Kirkbride 2016-04-08 14:14:36

I never specified that the solution should only work for a single line command.

@mayhem2k 2013-08-19 10:16:43

I tried this, all it did was open a cmd prompt with "cmd -c (my command)" and didn't actually run it. see below.

C:\windows\System32>cmd -c (powercfg /lastwake) Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved.

C:\windows\System32>

***Update
I changed my .bat file to read "cmd /k (powercfg /lastwake)" and it worked. You can also leave out the () and it works too.

@user2373716 2013-05-11 20:48:50

  1. Create new text file on desktop;

  2. Enter desired commands in text file;

  3. Rename extension of text file from ".txt" --> ".bat"

@Dan 2012-03-16 13:52:24

Yes. One option you have is to create a batch file containing the command

cmd -c {your command}

or

cmd -k {your command}

The shortcut will then be to this batch file.

@Arlen Beiler 2013-07-23 14:14:16

If I'm not mistaken, k = keep and c = close and specify what happens after the command is finished.

@Pacerier 2017-07-29 18:11:57

@ArlenBeiler, Did you guess that?

@Arlen Beiler 2017-07-29 20:00:29

I can't remember, but if you Google it you can find a command line reference for cmd online.

@Pacerier 2017-08-06 22:03:15

@ArlenBeiler, Well usually they don't tell what the shortcut stands for...

@Arlen Beiler 2017-08-07 01:42:49

No, they might not, but here is the link ss64.com/nt/cmd.html

@Todd 2018-02-05 00:07:59

For me, it had to be /K not -k.

@Eddie Kumar 2019-07-31 11:39:59

How to chain/pass multiple commands followed by a SINGLE "cmd" command, I tried the "&" sign to no avail. e.g. cmd hostname & whoami & dir c:\

Related Questions

Sponsored Content

24 Answered Questions

[SOLVED] Is there an equivalent of 'which' on the Windows command line?

15 Answered Questions

[SOLVED] Aliases in Windows command prompt

8 Answered Questions

[SOLVED] List all environment variables from the command line

45 Answered Questions

[SOLVED] How do I import an SQL file using the command line in MySQL?

14 Answered Questions

[SOLVED] How do I set a variable to the output of a command in Bash?

11 Answered Questions

[SOLVED] How to copy a folder from remote to local using scp?

17 Answered Questions

[SOLVED] How do I run two commands in one line in Windows CMD?

18 Answered Questions

[SOLVED] Run cURL commands from Windows console

12 Answered Questions

[SOLVED] Run Command Prompt Commands

21 Answered Questions

Sponsored Content