Posted on Leave a comment

Start windows service with powershell

Sometimes the windows services manager may not be enough for interaction with the services. Using the GUI you can set a service to start on startup by changing the status from Manual to Automatic but there could be cases that you want to execute this functionality with powershell. A reason for that could be a failure on the service startup that you want to check through code.

Using powershell you can check if a service is running with Get-Service. The below example checks if docker service is running and if not it will print a message on the output of the command line.

if (-not((Get-Service -Name "com.docker.service").Status -eq "Running")) { echo not running }

We can now check how to implement the start of the service using powershell. As shown below the docker desktop service is not running at the moment.

 if (-not((Get-Service -Name "com.docker.service").Status -eq "Running")) { Start-Service -Name "com.docker.service" }

After running the powershell above, we will get the service started.

Using the powershell you can create an automatic job with task scheduler and check this behavior on the computer startup.