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.
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.
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.
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 email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy