This article was originally posted on the blog.

Working With Windows Services in PowerShellPowerShell helps free up time by taking over certain service management tasks. See how to create such tools here.

Stopping, starting, and restarting Windows services is a common task for many IT professionals. We’re all used to the common services MMC snapin where we browse to a service, right-click on the service, restart it or double-click on it and change various attributes of a service. If we’re saving some time, we might even connect to a remote computer in the services snapin and manage them that way, but this method of working with Windows services soon becomes a pain.

It gets old, and wastes an IT pro’s valuable time, clicking around in a GUI when managing lots of services! Luckily, by using PowerShell, we can build tools to perform a lot of these service management tasks for us.

Ready to stop reading and start learning about PowerShell, DSC, Windows Server, Sharepoint, IIS and dozens of other categories? If so, check out the hundreds of free technical demo screencasts available on the new, IT career development platform TechSnips.

PowerShell gives us some commands right out of the box to work with Windows services.

We can leverage these built-in commands to perform all kinds of tasks to make our service management lives easier. For example, perhaps I’d like to somehow get the service status of lots of different computers at once from Active Directory. To create such a tool, I could leverage the Active Directory PowerShell module, pull the computers from AD, and then feed those computer names to the Get-Service command.

Read: Using The PowerShell Test-NetConnection Cmdlet On Windows

Now maybe I’d like to restart all of the services on all of those computers at once. To do that, I can pipe the results I just got directly to Restart-Service.

We could use the same approach for starting services or stopping services as well. Instead of using Restart-Service, I could have just as easily as used Stop-Service or Start-Service.

Services can also be modified using the Set-Service command. With the Set-Servicecommand, I can do things like change a service’s display name, startup type or description. Again, using the services we obtained with Get-Service earlier, I can pipe those services to the Set-Service command to perform some different actions.

You can see that as soon as you can query the services using Get-Service, just about anything can be done to those services. However, not everything can be done with the built-in commands. For instance, it’s not possible to change the username and password a service runs under using a Service command. Instead, we need to create our own method using WMI. To change service credentials, we’ll have to pull the service directly from WMI and invoke a .NET method on it to modify the service account. Here’s an excellent example of how to do that.

Even though everything’s not possible using the built-in service cmdlets, since PowerShell is built on top of .NET, just about anything is possible if you know where to look!

%d bloggers like this: