Windows·29 questions

How to manage Windows services via the command line and PowerShell?

Answer

Managing Windows services through the command line interface and PowerShell is a standard skill for any system administrator and developer. This allows you to automate the startup of background processes, configure their startup type, and quickly troubleshoot issues without needing to open the graphical Services management snap-in.

For basic service management in the classic command prompt, the `sc` utility is used. Working with it requires administrator privileges.

To start a stopped service, enter the command `net start service_name` or `sc service_name`.
To stop a running service, use the command `net stop service_name`.
To change a service startup type to automatic, run `sc config service_name start= auto`, and to disable the service, specify the `demand` or `disabled` value.

The PowerShell shell provides more modern and flexible cmdlets that return .NET objects, making it easier to filter and process data within scripts.

You can get a list of all services or the status of a specific service using the `Get-Service` cmdlet.
To start a service, use the `Start-Service -Name service_name` cmdlet.
You can stop a service using `Stop-Service`, and `Set-Service -Name service_name -StartupType Automatic` allows you to change the startup type.

Using PowerShell opens up possibilities for complex automation. For example, you can write a script that periodically checks the status of critical local database or web server services for a developer and automatically restarts them in case of a failure, sending a notification about the issue.

Was this answer helpful?

More questions in this topic

Related questions from other topics