Let’s continue our exploration of using PowerShell to manage services. If you’ve missed the previous articles, take a few minutes to get caught up. In this article I want to explain how to configure services using the Set-Service cmdlet. This cmdlet uses the same service object that Get-Service works with.

Setting Remote Status ^

In Part 3, I explained that commands like Stop-Service lacked a –Computername parameter. You can use the commands in a remote session using Invoke-Command, which is still a great way especially if you are managing services on multiple computers. But we can also start, stop, restart, pause and resume services remotely using 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 –WhatIf and you’ll need to use –Passthru to get any objects written 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 –Status are running, stopped, and paused. Be aware that if a service has dependencies you might not be able to modify the service as you can see in the screenshot.

set-service - Cannot stop service

set-service - Cannot stop service

Unfortunately, Set-Service lacks the –Force parameter so in these situations you will have to resort to my remoting example using Invoke-Command. If you want to restart a service, you’ll need a pipelined expression like this:

PS C:\> set-service w32time -ComputerName chi-dc03 -Status Stopped -PassThru | set
-service -PassThru -Status Running

Status   Name         DisplayName
------   ----         -----------
Running  w32time      Windows Time

Don’t forget to use –Passthru so an object is written to the pipeline, otherwise the second Set-Service command will have nothing to do.

Personally, I tend to run into more services that I can’t remotely stop using Set-Service, although starting is usually not a problem. I tend to stick using Invoke-Command. But remember, when using –Computername PowerShell is connecting using RPC and DCOM which can lead to firewall problems. Invoke-Command is using PowerShell remoting which you may not have configured or enabled anywhere, at least not yet!

Setting Startup Type ^

Where Set-Service comes in to play, is when you want to disable or enable a service using the –StartupType parameter. You can configure the service using the values Automatic, Manual or Disabled. Unfortunately, there isn’t an 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, there is no way to tell by looking at the service object what the startup type is.

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           :

There is a way, but I’ll cover that in the next article.

Be aware that changing the startup type doesn’t 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 disable and stop (or enable and start), pass the service object to the appropriate cmdlet.

PS C:\> set-service remoteregistry -StartupType Disabled -PassThru | Stop-Service 
-PassThru

Status   Name             DisplayName
------   ----             -----------
Stopped remoteregistry    Remote Registry

Summary ^

Technically, Set-Service permits you to modify a service display name and description but I’ve never had a situation where I needed to do that. I primarily use Set-Service to enable and disable services. If I need to manage a service remotely, I using Invoke-Command.

Everything I’ve shown in the last few articles has been using a specific type of service objects which as you’ve seen has some limitations. In the next article we’ll explore other ways of working with services that get around these limitations.

1 Comment
  1. Vivek 5 years ago

    set-service -ComputerName kcmsdff-Status Stopped -PassThru | set-service <<<<  -PassThru -Status Running

    + CategoryInfo          : OpenError: (System.ServiceProcess.ServiceController:ServiceController) [Set-Service], ServiceCommandException

    + FullyQualifiedErrorId : CouldNotStartService,Microsoft.PowerShell.Commands.SetServiceCommand

    Can you resolve this ? Or Please explain me  the problem

    Regards

    Vivek

Leave a reply

Please enclose code in pre tags

Your email address will not be published. Required fields are marked *

*

© 4sysops 2006 - 2021

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account