Run appcmd command from shortcut - windows

Very often I have to make following steps in command line (to identify correct worker process in IIS for debugging):
Run cmd as an administrator
cd %systemroot%\system32\inetsrv
appcmd list wp
I want to make a shortcut on the desktop to do it in one click.
How can I achieve that?
P.S. I tried to specify it like on the picture but it doesn't work, just opens cmd in inetsrv folder

I assume you set up the shortcut to run as admin, so I do not go into details on that.
What is missing in your Target line is the switch to tell cmd.exe that there are strings following which should be interpreted and executed as command. You can chose /C for execute and quit (cmd window disappears) or /K for execute and show prompt (cmd window stays open). So the result would be something like: "%windir%\system32\cmd.exe" /K "appcmd list wp". See cmd.exe /? for details.
GL, HF :)

Related

Run command on Command Prompt start

Is there a way to run any command (eg: cd D:\test) on Command Prompt start and to have this instance of Command Prompt as a different shortcut/instance.
You can create a shortcut that has a target of e.g.:
%comspec% /k ""C:\Full_Path_to\VsDevCmd.bat""
Where the .bat file is the command(s) that you want to run on startup. This will then leave you with an open command prompt after those commands have executed.
(In case it's not obvious, this is just shamelessly ripped from one of the Developer Command Prompt shortcuts that Visual Studio installs in the Start Menu)
%comspec% is a nice way of getting cmd to execute.
Cmd:
/k : Carries out the command specified by string and continues.

cmd.exe doesn't terminate when using a .bat file

[Context: I'm trying to create a shortcut to a .bat file with a relative "Start in" path as roughly described here and here.]
cmd.exe supports the /c switch. According to the documentation, this should cause it to "carry out the command and then terminate."
But the switch seems to be ignored when the command is a .bat file.
For example, if you create a shortcut with the following Target (to a normal, non-bat command):
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ notepad.exe test.txt"
Everything works as expected: Notepad opens and the console (shell) disappears. But if you replace the command above with a .bat file instead, like so:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat"
(where test.bat contains only "notepad.exe test.txt") Notepad opens as before but the console sticks around like an unwanted friend. Why? And more to the point, How do I make it go away?
UPDATE: I know I can use wscript, as in this solution, but then I lose the option of having a custom icon (I'm stuck with the default .vbs icon).
The start command begins a new process for the batch file. The original cmd.exe then terminates, but leaves the new process, which hangs around because it's waiting for notepad.exe to terminate.
Change your bat file contents to:
start "" notepad.exe test.txt
Then your batch file will not wait for notepad to exit before continuing execution.
Another thing to try:
C:\Windows\System32\cmd.exe /c "START /d C:\temp\ C:\test.bat & exit"
The nuclear option would be to write a small program in the (compiled) language of your choice that launches the .bat file and then exits. Then you can give it a custom icon, and make it do whatever you like.
You might also take a look at Autoit from http://autoitscript.com as an alternative to batch. - the Run() command can do this kind of thing with better predictability. Since it makes an executable you can link this from a shortcut directly. You can also do a whole lot more of course, like run as a different user, insert delays or handle errors, that are hard to do with batch.
You don't need the full kit, just the Aut2EXE folder from the download will do.
BTW, build your exes without UPX compression as that leads to AV false positives.
I'm a little late but here is the answer.
The documentation for start states:
Syntax
START "title" [/D path] [options] "command" [parameters]
If command is an internal cmd command or a batch file then the command
processor is run with the /K switch to cmd.exe. This means that the
window will remain after the command has been run.
If start is used to execute a batch file, the opened cmd instance wont close.
You could also use call instead.
call C:\test.bat

Run a Command Prompt command from Desktop Shortcut

Is it possible to create a desktop shortcut that, when pressed, will open command prompt and run a pre-defined command?
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:
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".
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
The solutions turned out to be very simple.
Open text edit
Write the command, save as .bat.
Double click the file created and the command automatically starts running in command-prompt.
Create new text file on desktop;
Enter desired commands in text file;
Rename extension of text file from ".txt" --> ".bat"
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.
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
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.
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
Right click on Desktop.
Enter the command in "Type the Location of Item" bar.
Double click the shortcut to run the command.
Found detailed Instructions here
Using the Drag and Drop method
From the windows search bar type in cmd to pull up the windows bar operation.
When the command line option is shown, right click it and select Open File Location.
The file explorer opens and the shortcut link is highlighted in the folder. If it is not highlighted, then select it.
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.
Drop the link on the desktop.
Change properties as needed.

CMD Script: How to close the CMD

I have created a small command that will let me launch Internet Explorer. However, I wish to close the small command prompt that shows up when I launch IE. How can I do this? This is my current code:
"%ProgramFiles%\Internet
Explorer\iexplore.exe"
http://localhost/test.html
PAUSE
I am guessing if I take out the Pause. It will close the CMD box upon closing IE??
Also is there another command that I can use to simply create a command that will let me add something to the Menu with a small icon, which in turn runs the above. Is this complicated? Any tutorials I can use?
Thanks all
Use the start command:
start "title" "%ProgramFiles%\Internet Explorer\iexplore.exe" http://www.example.com
you need this on the end
&& exit
For example
"%ProgramFiles%\Internet Explorer\iexplore.exe" http://google.co.uk && exit
#echo off
start "" "%ProgramFiles%\Internet Explorer\iexplore.exe" "http://www.example.com"
exit /b
But you really should not force IE, but use the default browser:
#echo off
start http://www.example.com
exit /b
exit /b does not work on win9x IIRC, so if you need to support every version of windows and close the terminal window if the user double clicks your batch file, go with:
#echo off
start http://www.example.com
cls
You can also launch your program with the /c switch, which terminates the cmd once its finished executing
for example
cmd /c "%ProgramFiles%\InternetExplorer\iexplore.exe" http://localhost/test.html
You have to add 'start' in front of every program you launch, elsewhere your script is going to wait until it's finished.
A little late here, but running it in minimized mode or invisible mode might be another option. Source: https://www.winhelponline.com/blog/run-bat-files-invisibly-without-displaying-command-prompt/
Running .BAT or .CMD files in minimized mode
To run a batch file in a minimized window state, follow these steps:
Create a shortcut to the .BAT or .CMD file. To do so, right click on the file, click Send To, Desktop (create shortcut)
Right click on the shortcut and choose Properties
In the Run: drop down, choose Minimized
Click OK
Double-click the shortcut to run the batch file in a minimized window state.
"Mind the gap!"
Command Prompt always takes the empty space as separator, unless it's enclosed in double quotes.
So, if any Path, or Program/File Name, or anything includes empty space/es, must closed in quotes.
eg. "C:/Program files/..." path/directory or "Any Program/Command/File.exe/cmd/txt..." Program/Command/File Name includes space/es.
Syntax:
> start /?
Starts a separate window to run a specified program or command.
START ["title"] [/D path] (start swiches here...) [command/program] (com/prog-parameters here)
start "" /d "Drive:/the/Program/Path/..." "Command/Program Name.extension" "File-Name.extension"
So, it's usual fault:
If you don't set the 1st set of quotes "" for title (even if there's nothing to enclose), then the START command takes whats inside the 1st quotes set (eg. path! or Program Name!) and sets it as title... and of course, it messing up.

How can I make a shortcut start in a different directory when running it as an administrator on Windows Vista?

I have a shortcut on my desktop which opens a command prompt with many arguments that I need. I set the 'start in' field to d:\ and it works as expected (the prompt starts in d:). When I choose Advanced -> run as administrator and then open the shortcut, it starts in C:\Windows\System32, even though I have not changed the 'start in' field. How can I get it to start in d:\?
If you use the /k argument, you can add a single line to execute a change drive and change directory. For instance:
C:\Windows\System32\cmd.exe /k "d: & cd d:\storage"
Using & you can string together many commands on one line.
Edit: You can also change drive with the cd command alone "cd /d d:\storage". Thanks to Adam Mitz for the comment.

Resources