Start and Stop SQL Server Using a Script

November 4, 2017

This is a simple CMD script that will start or stop SQL Server. SQL Server can be a memory and processor hog so I usually set my SQL Server startup to Manual and use the script to toggle its running state. You will need to run the script as administrator.

echo off

for /F "tokens=3 delims=: " %%H in ('sc query "MSSQLSERVER" ^| findstr "        STATE"') do (
  if /I "%%H" NEQ "RUNNING" (   
   net start MSSQLSERVER
  )
  if /I "%%H" NEQ "STOPPED" (   
   net stop MSSQLSERVER
  )
)

PAUSE

Hopefully this will be helpful to others…


Discussion, links, and tweets

My name is Deon Heyns and I am a developer learning things and documenting them in realtime. Python, Ruby, Scala, .NET, and Groovy are all languages I have written code in. I appeared in the New York Post once. I host my code up at GitHub and Bitbucket so have a look at my code, fork it and send those pull requests.

comments powered by Disqus