Run a Command Prompt command from Desktop Shortcut
Asked Answered
G

8

109

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

Geralyngeraniaceous answered 16/3, 2012 at 13:48 Comment(0)
G
40

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

Geralyngeraniaceous answered 16/3, 2012 at 15:28 Comment(10)
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.Milline
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.Geralyngeraniaceous
You could just put in the batch file: @echo off cd "%HOMEDRIVE%/Your/Path/To/program.exe" How is this the accepted answer, though?Winepress
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.Geralyngeraniaceous
@nashwan I interpret "shortcut" is a clickable icon on the desktop.Geralyngeraniaceous
@PhilipKirkbride I can see that :-)Desantis
See the top voted response above. This "answer", while it does work, is much more complex than needed.Guerrilla
I like the approach, but I don't see how this actually runs anything in the command prompt.Autonomic
if copying screenshot from other answer and pasting your own, I do not think my answer need to be hereBaresark
I have to add since the OP question is general. For running commands requiring administrator privileges from .bat file refer to this post #1895467Sphere
I
198

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

Inexactitude answered 12/9, 2014 at 21:39 Comment(6)
you can type cmd instead of the entire pathMizuki
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).Farrison
I use this method to open a command prompt then run a "setup" batch command file to setup parameters for future work.Euchre
@JoeCotton can you elaborate on this? It seems like that would be a good response to the problem that once you have created the shortcut, it is really inconvenient to edit it.Autonomic
Reply to @Kukas You want to edit a short cut? Right click on the shortcut and choose Properties from the dropdown.Euchre
I found that if you needed to use a path in your command and that path had spaces you need to surround the path with double quotes and surrounding the whole command with brackets like this: cmd /c ("C:/Users/My name is Bruh/Desktop/java.exe" "C:/Program Files/bruhScript")Reputable
L
53

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
Lightless answered 16/3, 2012 at 13:53 Comment(1)
I found that if you needed to use a path in your command and that path had spaces you need to surround the path with double quotes and surrounding the whole command with brackets like this: cmd /c ("C:/Users/My name is Bruh/Desktop/java.exe" "C:/Program Files/bruhScript")Reputable
G
40

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

Geralyngeraniaceous answered 16/3, 2012 at 15:28 Comment(10)
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.Milline
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.Geralyngeraniaceous
You could just put in the batch file: @echo off cd "%HOMEDRIVE%/Your/Path/To/program.exe" How is this the accepted answer, though?Winepress
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.Geralyngeraniaceous
@nashwan I interpret "shortcut" is a clickable icon on the desktop.Geralyngeraniaceous
@PhilipKirkbride I can see that :-)Desantis
See the top voted response above. This "answer", while it does work, is much more complex than needed.Guerrilla
I like the approach, but I don't see how this actually runs anything in the command prompt.Autonomic
if copying screenshot from other answer and pasting your own, I do not think my answer need to be hereBaresark
I have to add since the OP question is general. For running commands requiring administrator privileges from .bat file refer to this post #1895467Sphere
S
15
  1. Create new text file on desktop;

  2. Enter desired commands in text file;

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

Sell answered 11/5, 2013 at 20:48 Comment(0)
S
11

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.

Servomotor answered 16/3, 2012 at 13:52 Comment(6)
If I'm not mistaken, k = keep and c = close and specify what happens after the command is finished.Executory
@ArlenBeiler, Did you guess that?Pilcher
I can't remember, but if you Google it you can find a command line reference for cmd online.Executory
@ArlenBeiler, Well usually they don't tell what the shortcut stands for...Pilcher
For me, it had to be /K not -k.Verbena
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:\Doorkeeper
B
6

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

Blockhead answered 21/5, 2014 at 13:16 Comment(0)
W
3

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.

Wirehaired answered 19/8, 2013 at 10:16 Comment(0)
P
0

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.
Pelmas answered 2/3, 2017 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.