Three Cool PowerShell Service Tricks

Three Cool PowerShell Service Tricks

  • Comments 8
  • Likes

Summary: Ed Wilson, the Microsoft Scripting Guy, talks about three cool Windows PowerShell tricks for working with services.

Microsoft Scripting Guy, Ed Wilson, is here. It does not matter what ones role is, whether an Exchange Server admin, a Group Policy guru, or even an ordinary every day power user, one eventually needs to work with services. Today I will show three cool tricks that involve using Windows PowerShell to work with services.

Stop multiple services at once

One of the nice features of the Get-Service cmdlet is that it accepts an array. Therefore, I can find information about specific services by supplying the name of multiple services. This technique is shown here.

PS C:\> Get-Service bits,wuauserv

 

Status   Name               DisplayName

------   ----               -----------

Running  bits               Background Intelligent Transfer Ser...

Running  wuauserv           Windows Update

The Stop-Service cmdlet accepts an array of ServiceController objects (the type of object returned by the Get-Service cmdlet). Therefore, I can pipe the results of the Get-Service cmdlet to the Stop-Service cmdlet to stop the array of services. This command is shown here.

Get-Service bits,wuauserv | Stop-Service

To see what a command does prior to the actual execution of the command, use the WhatIf switch. In the image that follows, the use of the WhatIf switch to prototype the command appears, followed by the actual command to stop the services.

Image of command output

Start multiple services

The Start-Service cmdlet also accepts an array of ServiceController objects as pipelined input. This means that I can use Get-Service to look at my service status, and then pipe the objects to the Start-Service cmdlet if required. This command is shown here.

Image of command output

Stopping and disabling services in one command

A cool thing is that I can use the Get-Service cmdlet to check on a group of services. If I want to stop them, I can pipe the returned ServiceController object to the Stop-Service cmdlet. But what if I also do not want the services to start up on the next reboot? Here is where some really cool stuff comes into play…

I use the PassThru switch, and the ServiceController objects will pass along the pipeline. I can then use those objects with the Set-Service cmdlet and change the startup type of the services to Disabled. Here is the command to accomplish this task.

Get-Service bits,wuauserv | Stop-Service -PassThru | Set-Service -StartupType disabled

Unfortunately, although Set-Service permits setting the startup type of the service to Disabled, the Get-Service cmdlet does not report the startup type of the service. To check on the results of our command, I need to use WMI. Here is the WMI command that I use to check the start mode of the services.

gwmi win32_service -filter "name = 'bits' OR name = 'wuauserv'"

The command and the associate output from the command are shown in the image that follows.

Image of command output

Join me tomorrow when I will talk about more way cool Windows PowerShell stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy 

Your comment has been posted.   Close
Thank you, your comment requires moderation so it may take a while to appear.   Close
Leave a Comment
  • Hi Ed,

    interestingly enough, we can work with multiple services in one command!

    That's something to keep in mind ... using array parameters is often an option!

    Having followed and executed your 3 cool tricks, I would llike to add at least the fourth trick to it

    because you left us with two disabled and stopped services:

    4. "Enable and start multiple services in one command"

    Get-Service bits,wuauserv | Set-Service -StartupType automatic -PassThru | Start-Service

    I hope this helps :-)

    Klaus.

  • Why doesn't Get-Service return the startup type ?  Would have thought that a basic property ?

  • @Klaus Schulte thanks, yes starting two services back up is a good idea to do after disabling them. I appreciate the addition.

  • @ratty67 PowerShell does not return startup type via the Get-Service cmdlet because it relies on the ServiceController object from the .NET Framework. This class does not contain the information, and therefore the cmdlet does not return it. I am not sure why the ServiceController class does not contain the information.

  • @ratty67

    you can use WMI instead of serviceController, or use this with PS3 to extend types directly:

    PS III> # untested:

    PS III> $props = @{

     TypeName =  'System.ServiceProcess.ServiceController'

     MemberName = 'StartUpMode'

     MemberType = 'ScriptProperty'

     Value = { (gwmi win32_service -filter "name='$($this.name)'").startMode }

     DefaultDisplayPropertySet = Status,Name,DisplayName,StartUpMode

    }

    PS III> Update-TypeData @props

  • Much more useful than the older sc.exe and 'net stop' methods.

  • how would i restart a service or list of services while taking the "Stopping Service" and "Starting Service" and sending it to a txt file like RestartServices.txt?

  • Justin C Use a transcript. or break the pipeline into a set of "foreach-object" sections and log to a file.