How to Work with Services in PowerShell

Last Update: Nov 19, 2024 | Published: Jan 11, 2021

SHARE ARTICLE

Services run many different aspects of Windows and are used for a great many tasks. How then can you use PowerShell to work with these services, whether it is existing services, creating new services, or to remove existing services? This article covers how PowerShell can be used to work services in Windows using PowerShell!

What built-in commands can we use PowerShell? Listing all the commands available in the Microsoft.PowerShell.Management module shows us the following.

Untitled 2020 12 24T190038.911

These cover virtually all of the tasks that we would want in a service. Read on to learn how we can use these commands!

Retrieving and Manipulating Services in PowerShell

The first task that we need to tackle is how to enumerate and list all of the available services. Using the Get-Service command we can retrieve all known services or just one. Passing in a single service name allows us to find out the status of that service, such as the Print Spooler service of Windows.

Get-Service -Name 'spooler'
Untitled 2020 12 24T190109.355

What about listing all of the properties of a given service? Simply pipe the result into Select-Object * to see all of the available options.

Get-Service -Name 'spooler' | Select-Object *
Untitled 2020 12 24T190125.635

There are a lot of properties that you would not typically be able to see using the GUI interface, notably some of the abilities such as CanPauseAndContinue.

Stopping, Starting, and Restarting a Service

To stop, start, and restart a service, the following commands are available to you.

  • Stop-Service – This will stop the service, but if there are dependent services, you will need to use the Force parameter switch.
  • Start-Service – Use this to start a service, provided it is of the start type of Manual, Automatic, or Automatic (Delayed Start). This will not work with Disabled services, but you can use Set-Service to change the start type first.
  • Restart-Service – This command will first stop the service and then start the service. If the service is already stopped it will start without notification. Use the Force parameter switch to stop the service with dependent services.
Stop-Service -Name 'spooler'
Start-Service -Name 'spooler'
Restart-Service -Name 'spooler'

If you do not have permission to a service, you will get an error that says: Cannot open 'service' service on computer '.'. Replace service with the expected service name, and . with the computer this is operating on, . indicates the local computer.

Creating a New Service

What if you have an executable that you want to run as a service? In the past, this used to be more difficult and required the use of the sc.exe command built-in to Windows. Using the New-Service command, you can quickly and easily create new Windows services, provided the executable you are targeting supports this.

$Params = @{
	'Name'           = 'TestService'
	'BinaryPathName' = 'C:\Path\To\My\executable.exe'
	'Description'    = 'This is a test service!'
	'StartupType'    = 'Automatic'
	'Credential'     = (Get-Credential)
	'DependsOn'      = 'NetLogon'
}

New-Service @Params

As you can see, it is pretty easy to create a new service. You can create the service with a startup type, credential, and even define a dependent service (or services, if passed an array).

Modifying a Service

What if you have an executable that you want to run as a service? In the past, this used to be more difficult and required the use of the sc.exe command built-in to Windows. Using the New-Service command, you can quickly and easily create new Windows services, provided the executable you are targeting supports this.

Set-Service -Name 'TestService' -Credentials (Get-Credential) -Status 'Stopped'

Sometimes mistakes are made, so how do we go about removing a service? Read on to learn more!

Removing a Service

Finally, you may find that you need to remove a service, which can be done using the appropriately named Remove-Service. This was introduced in PowerShell 6.0 and must be run as an administrator.

Remove-Service -Name 'TestService'

This command actually removes the service from both the services database and in the registry. There are two other service commands, which are not used often, so read on how you might use them!

Bonus! Suspending and Resuming a Service

There are certain services that can be suspended and then resumed using the Suspend-Service and Resume-Service commands. These commands only work on services that have $True set for the CanPauseAndContinue property. To find all of these services that support this, you can use the following command to find all that support this function.

Get-Service | Where-Object {$_.CanPauseAndContinue -eq $true} | Select-Object Name

For example, the LanmanWorkstation service supports this property.

Suspend-Service -Name 'LanmanWorkstation'
Resume-Service -Name 'LanmanWorkstation'

You may need to run your PowerShell session as an administrator to control the services if you receive an error when running against a supported service.

Conclusion

PowerShell over the years has introduced more ways to work with and manage Windows services. Although PowerShell 6+ is cross-platform, the support for managing services is not yet supported in all environments. Use the available commands to quickly and efficiently manage, create, and remove services on your systems today!

SHARE ARTICLE