Is it possible to create a desktop shortcut that, when pressed, will open command prompt and run a pre-defined command?
This question is related to
windows
command-line
command-prompt
shortcuts
Using the Drag and Drop method
cmd
to pull up the windows bar operation.Open File Location
.Copy to Desktop
while dragging and before dropping, then push down and hold the Control key until you see the message. 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
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
Create new text file on desktop;
Enter desired commands in text file;
Rename extension of text file from ".txt" --> ".bat"
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 runYes. 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.
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.
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:
Right click on some empty space in Explorer, and in the context menu go to "New/Shortcut".
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
".
Source: Stackoverflow.com