Manage Learn to apply best practices and optimize your operations.

Ten hidden Windows command prompt tricks

2/12

Run multiple Windows commands from the command prompt

Source:  Nirmal Sharma; Photograph: Getty Images/iStockphoto

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

Join the conversation

1 comment

Send me notifications when other members comment.

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy

Please create a username to comment.

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.

So....

DIR C:\FunnyDir & ECHO Done

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

And

DIR C:\FunnyDir && ECHO Done

Will only ECHO Done if FunnyDir Exists

And

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

-ADS BY GOOGLE

SearchVirtualDesktop

SearchWindowsServer

SearchExchange

Close