
We continue to learn how to manage Windows services using PowerShell. In a
previous post, we looked at how to get the status of a service on a local and remote computer, filter the services (for example, find only stopped services) and identify dependent services. In this post such rather trivial things as will be considered:
- Service stop
- Service start
- Restart service
- Suspending and resuming work
- Remote Services Management
- Setting up service startup
We will pay more attention to parsing commands in PowerShell to implement the above listed on the local computer. In the section “Managing Remote Computer Services,” we will look at work limitations in PowerShell v2 and v3. Details under the cut.
Previous article:Manage Windows services using PowerShell. Part 1. Get the status of servicesPS C:\> get-service bits Status Name DisplayName ------ ---- ----------- Running bits Background Intelligent Transfer Ser...
Since the command for obtaining the status of a service is called
Get-Service , guessing how other commands are written is not difficult. At worst, we can ask PowerShell about all the commands related to working with services. Notice that we used the
–noun parameter to get all the commands associated with the services.
')

Take a closer look at these commands.
STOP SERVICE
To stop a service, we need to clarify its name.
PS C:\> stop-service wuauserv
However, nothing will be transferred to the conveyor. Some cmdlets, such as
Stop-Service , are designed in such a way that, by default, they do not write an object into the pipeline. We will do this by using the
–Passthru parameter.
PS C:\> stop-service bits -PassThru Status Name DisplayName ------ ---- ----------- Stopped bits Background Intelligent Transfer Ser...
If the service is not running, the cmdlet will not output anything, nor will it produce any error. Therefore, it is sometimes better to transfer the object to the
Stop-Service (naturally using the
–whatif parameter).
PS C:\> get-service browser | stop-service -WhatIf What if: Performing operation “Stop-Service” on Target “Computer Browser (browser)”.
The
–WhatIf parameter
has been added so that we can see what happens if the cmdlet is launched. When I make sure that this is the service that interests me, I will simply delete
-Whatif and stop the service.
PS C:\> get-service browser | stop-service
As I mentioned above, if the service is already stopped, the cmdlet will not do anything. And the use of
Stop-Service in this case does not harm anyone. However, I still prefer a more civilized approach, namely:
PS C:\> get-service bits | where {$_.status -eq
If the service is running, the object is passed to the pipeline and sent to the
Stop-Service . Below is an option with stopping multiple services.
PS C:\> get-service bits,wsearch,winrm,spooler | where {$_.status -eq
Some services will not want to stop - due to the presence of dependent services - which we see in the screenshot below.

In this case, use the parameter
–Force . In most cases, this works, but without the “foolproof”. Remember that the command will also stop dependent services.
PS C:\> stop-service lanmanserver -force –PassThru Status Name DisplayName ------ ---- ----------- Stopped Browser Computer Browser Stopped lanmanserver Server
START-SERVICE
The service is started in the same way. It supports the
–Whatif parameter, and you have to use
–Passthru to see the objects.
PS C:\> start-service wuauserv -PassThru Status Name DisplayName ------ ---- ----------- Running wuauserv Windows Update
And again: if the service is already running, the cmdlet will not do anything. However, you can try to start the service and get this error.

The reason for this is in most cases the services that are disabled. How to configure service settings, I will explain in the next article.
If you want to start services and all services dependent on it, use the following expression:
PS C:\> get-service lanmanserver | Foreach { start-service $_.name -passthru; start-service $_.DependentServices -passthru} Status Name DisplayName ------ ---- ----------- Running lanmanserver Server Running Browser Computer Browser
We must explicitly get the dependent services, because the
Start-Service will not automatically start them.
RESTART-SERVICE
You'd be surprised, but restarting the service also works like the two previous examples. Use
–Passthru if you want to make sure that the service is running.
PS C:\> restart-service spooler -PassThru Status Name DisplayName ------ ---- ----------- Running spooler Print Spooler
Since we are stopping the service, we may need the
–Force parameter.
SUSPENSION AND RENEWAL OF WORK
Some services may be suspended for some time and then resumed, and we can do this through PowerShell. However, if the service does not meet the requirements, we will get such errors. (the example shows that we tried to suspend the bits service)

What is the problem? We look at the object (using
Get-Service ).
PS C:\> get-service bits | select * Name : bits RequiredServices : {RpcSs, EventSystem} CanPauseAndContinue : False CanShutdown : False CanStop : True DisplayName : Background Intelligent Transfer Service DependentServices : {} MachineName : . ServiceName : bits ServicesDependedOn : {RpcSs, EventSystem} ServiceHandle : SafeServiceHandle Status : Running ServiceType : Win32ShareProcess Site : Container :
If the value of the
CanPauseAndContinue property is
True , then we can pause and resume the service. Find the following services:
PS C:\> get-service | where {$_.CanPauseandContinue} Status Name DisplayName ------ ---- ----------- Running LanmanServer Server Running LanmanWorkstation Workstation Running MSSQLSERVER SQL Server (MSSQLSERVER) Running O2FLASH O2FLASH Running stisvc Windows Image Acquisition (WIA) Running Winmgmt Windows Management Instrumentation
As we see, not many services satisfy this requirement.
PS C:\> suspend-service o2flash -PassThru Status Name DisplayName ------ ---- ----------- Paused O2FLASH o2flash
Ready to resume service? Use the following expression:
PS C:\> resume-service o2flash -PassThru Status Name DisplayName ------ ---- ----------- Running O2FLASH o2flash
Both cmdlets also support
–Whatif .
REMOTE SERVICES
As you could notice, we have demonstrated all the examples above on a local machine. And it is no coincidence. Unfortunately, even in PowerShell v3, none of these cmdlets have a parameter that allows you to manage the service on a remote computer.
Get-Service , of course, supports the
–Computername parameter, but no more. You will be able to contemplate the service, but you will not be able to do anything with it. No, you can, of course, if the remote computer works with PS v2 and PowerShell Remoting is enabled. Then we can use all the above commands using
Invoke-Command for a remote computer or
PSSession . On the other hand, it is easier to manage the same service across multiple servers.
PS C:\> Invoke-Command {restart-service dns –passthru} –comp chi-dc03,chi-dc02,chi-dc01

The management of services on remote computers is not limited to the above, but this will already be the subject of further articles.
All these cmdlets can be used in a pipeline, and often this is the best option. Use
Get-Service to get objects and then pass them to the appropriate cmdlet.
WE INSTALL A REMOTE STATUS
So, we found out that the
Stop-Service cmdlet does not have such a useful parameter as
–Computername . You can use these commands in a remote session by accessing the
Invoke-Command cmdlet, which is in itself productive if you work with the service on several computers. One can be started, stopped, restarted, paused and restarted using the
Set-Service .
PS C:\> set-service wuauserv -ComputerName chi-dc03 -Status stopped -WhatIf What if: Performing operation "Set-Service" on Target "Windows Update (wuauserv)".
This command supports the
–WhatIf parameter. You must also use
–Passthru to transfer objects to the pipeline.
PS C:\> set-service bits -ComputerName chi-dc03 -Status running -PassThru Status Name DisplayName ------ ---- ----------- Running bits Background Intelligent Transfer Ser...
Valid values ​​for the
–Status parameter are “running” (running), “stopped” (stopped), and “paused”. Remember that the service has dependent services, we can not change it, as shown in the screenshot below.

Unfortunately,
Set-Service does not have the
–Force parameter, so you have to go back to using PowerShell remoting and
Invoke-Command . If you want to restart the remote service, use the following command:
PS C:\> set-service w32time -ComputerName chi-dc03 -Status Stopped -PassThru | set-service -PassThru -Status Running Status Name DisplayName ------ ---- ----------- Running w32time Windows Time
Do not forget to use
–Passthru , otherwise the second
Set-Service command will not do anything.
As for me, I prefer to work with several services at once, which I cannot remotely stop using Set-Service, although their launch of problems is. I use
Invoke-Command . But remember that using the
–Computername option PowerShell connects using RPC and DCOM, which can lead to problems with the firewall.
Invoke-Command uses PowerShell remoting, which we may not have yet configured or enabled.
WE INSTALL A SERVICE STARTUP TYPE
Set-Service is useful when you want to enable or disable a service using the
–StartupType parameter. If you configured the service using the values ​​Automatic, Manual or Disabled. Unfortunately, there is no option for Automatic (Delayed).
PS C:\> set-service remoteregistry -StartupType Manual -WhatIf What if: Performing operation "Set-Service" on Target "Remote Registry (remoteregistry)". PS C:\> set-service remoteregistry -StartupType Manual -PassThru Status Name DisplayName ------ ---- ----------- Stopped remoteregistry Remote Registry
However, just looking at the object, we can not say what type of autoload it belongs to.
PS C:\> get-service remoteregistry | select * Name : remoteregistry RequiredServices : {RPCSS} CanPauseAndContinue : False CanShutdown : False CanStop : False DisplayName : Remote Registry DependentServices : {} MachineName : . ServiceName : remoteregistry ServicesDependedOn : {RPCSS} ServiceHandle : SafeServiceHandle Status : Stopped ServiceType : Win32ShareProcess Site : Container :
How to do this is one of the topics of the next article.
Remember that changing the type of startup will not affect the current status of the service.
PS C:\> set-service remoteregistry -StartupType Disabled -PassThru Status Name DisplayName ------ ---- ----------- Running remoteregistry Remote Registry
So if you want to shut down and stop (or enable and start) a service, pass the object to the appropriate cmdlet.
PS C:\> set-service remoteregistry -StartupType Disabled -PassThru | Stop-Service -PassThru Status Name DisplayName ------ ---- ----------- Stopped remoteregistry Remote Registry
Technically,
Set-Service allows you to change the service display name and description, but personally I have never had to use it in my work. I use
Set-Service to turn services on and off. If I need to manage services remotely, then I use
Invoke-Command .
All that I have demonstrated in recent articles has been associated with the use of specific types of service objects, which, as you may have noticed, have some limitations. In the next article, we will look at other service management capabilities that are designed to circumvent these limitations.
Upd:The post contains translations of articles from the portal
4sysops.comManaging Services the PowerShell way - Part 3Managing Services the PowerShell way - Part 4