Last Update: Nov 19, 2024 | Published: Jan 11, 2021
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.
These cover virtually all of the tasks that we would want in a service. Read on to learn how we can use these commands!
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'
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 *
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
.
To stop, start, and restart a service, the following commands are available to you.
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 '.'
. Replaceservice
with the expected service name, and.
with the computer this is operating on,.
indicates the local computer.
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).
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!
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!
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.
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!