You can run multiple Windows commands in one go from the command prompt. For example, you can start or stop a service from a command prompt by typing "Net Start/Stop." What if you need to restart a service from the command prompt? In that case, you can use a double ampersand -- && -- which allows you to run multiple "Net" commands in one line to first stop and then restart the service.

View All Photo Stories

1 comment FormatSeeColon - 29 Jul 2015 4:15 PM
Once again, the ampersand and double ampersand is misrepresented.

Use a single ampersand to chain commands together so that they will ALL execute.

If you use the double ampersand, the second command will ONLY execute if the first one is successful.

Similarly, if you use a double vertical bar, the second command will only execute if the first one fails.

DIR C:\FunnyDir & ECHO Done

Will ECHO Done no matter if C:\FunnyDir exists or not.

DIR C:\FunnyDir && ECHO Done

Will only ECHO Done if FunnyDir Exists

DIR C:\FunnyDir || ECHO Done

Will only ECHO Done if C:\FunnyDir doesn't exist.

The double bars are very useful for flow control. An example that I use involves connecting to a VPN and launching an RDP session if the connection works:

RASDIAL remote userid password || GOTO :EOF
START /MAX /WAIT MSTSC /V remoteserver
RASDIAL remote /disconnect

If the first RASDIAL fails, the batch file exists peacefully (GOTO :EOF). If the first RASDIAL is successful, and RDP session is started and the RASDIAL session will be disconnected when the RDP session ends.
[null, {"document_url": "https://searchenterprisedesktop.techtarget.com/photostory/2240216612/Ten-hidden-Windows-command-prompt-tricks/2/Run-multiple-Windows-commands-from-the-command-prompt", "unformatted_src": "https://cdn.ttgtmedia.com/rms/onlineImages/sED_10_hidden_command_02.jpg", "src": "https://cdn.ttgtmedia.com/rms/onlineImages/sED_10_hidden_command_02.jpg", "formatted_filename": "sED hidden command"}, null, null]