Manage SQL Server services with Powershell


I tend to use SQL Server on my laptop to test out new features and develop and test scripts. I’ve tried creating a VM for this but everything slows down too much and I keeps me from finishing stuff. I have the patience of a humming bird so waiting for a few minutes feels like a lifetime.

When you install SQL Server you get the choice to set up the way the SQL Server services are started. You can select automatic, manual, disabled etc.
The services on my laptop are set on manual because I don’t use SQL Server all the time.

The downside is that I either have to go to the services of Windows or execute a command from a command prompt and keep in mind that it is in administrator mode.

It’s possible to start and stop the SQL Server services with powershell so that’s going to be my solution.

One of the things you want the script to do is check if the window where it’s started, is in elevated (administrator) mode. If it’s not you can’t start the services. Benjamin Armstrong made a nice self-elevating script that will check the elevation and restart the script in elevated mode to make sure the script is executed succesfully.

Here is the code for self elevating the window:

The next part is easier where we have to create a managed computer object.

Next up is get the services into variables:

At last we start the services by checking of they are in the right state and execute the Start() or Stop() command. I only included the SQL Service and the SQL Agent but you can easily add the other services of SQL Server.

For starting the services:

For stopping the services:

To dot the i’s I included a command to wait for a keystroke to close the window:

The final result can be seen below:



This all made starting and stopping my SQL Server services a lot easier. Hope you find this useful and any comment is appreciated.

Leave a Reply