Using Powershell scripts to start and stop Windows Services

Using Powershell scripts to start and stop Windows Services

If you have a slow computer and want to save some memory or processing when you are not coding, this is for you

As a developer, once you finish setting up your development environment, you are going to have services running in the background. And depending on what kind of developer you are or the tools you have, you will have them taking up resources. That's fine if your primary use is software development. But what if you also use it as a gaming machine or, say, this is the family computer? Would you need to have the SQL Server service running all the time?

I know I wouldn’t.

Since I got a new machine I decided to investigate on how to make it easier to start and stop services and I realized that doing it through PowerShell would help me. Let's say that I would want to stop the SQL Server. I would use PowerShell in this way.

  1. Open PowerShell as administrator
  2. Get the service, just to make sure
    Get-Service -Name 'MSSQLSERVER'
  3. Then stop it
    Stop-Service 'MSSQLSERVER'

And the same for whenever I would start the service (Start-Service 'MSSQLSERVER').

There are 2 ways to get the name of the service in your machine:

  1. Via PowerShell
    Get-Service
  2. Via Services
    Win+R (Run), type services.msc, double click on the service and check the Service Name
    mssqlserver-name.png

But then I thought: How can I scale this? Because, see, I currently have 3 services I don't want to have enabled all the time. Maybe next week I will have 10. How can I know?

I ended up creating 2 PowerShell scripts for that. Yes, I know, I needed only one and I could pass start or stop as parameter. But my PowerShell skills are very poor so I did what I could. Maybe I will come back to that later. Or maybe you can help, why not? 😉

The scripts are on my GitHub, more precisely in this repository: https://github.com/davidsonsousa/Start-Stop-Dev-Services

Great! It's automated enough. But I needed more.

The next step was to create a shortcut for each script in the desktop. Since I need to execute the scripts as Administrator, I needed to create a very special kind of shortcut. Something like this:

C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\Foo\Bar\XXXX-services.ps1" -WindowsStyle Hidden

Notice the part in bold: This is where I have the path and name of the script.

Added some fancy icons to reminded me what is what:

script-shortcuts.png

Et voilà. Now I can start or stop my services whenever I want:

script-stopped.png

Important to note that the warning on the screenshot comes from RabbitMQ itself. Other services may have the same kind of message, which is fine.