Checking and Setting the Start Mode of a Windows Service with PowerShell

Working with Services in PowerShell is probably one of the first tasks you’ll undertake as a PowerShell newbie. Recently I needed to disable a few Windows services on a server and I’m a big believer in being able to get back to where you started so the first thing I set out to accomplish was to find out what the start mode was of the services I needed to change.

My demo machine doesn’t have those particular services I referenced so the BITS service will be used for the demonstration in this scenario:

2014-04-13_19-14-48

By default only a few properties are displayed, but like most things in PowerShell, there’s more properties available than are returned by default. I’ll simply pipe the previous command to the Select-Object cmdlet, specify the -Properties parameter and use the wildcard character (asterisk) to return all properties and their values for the BITS service:

2014-04-13_19-21-23

As you can see in the previous results, there’s still isn’t a StartMode parameter. You can certainly change the StartMode of a service from within PowerShell with the Set-Service cmdlet, so why can’t you check to see what the StartMode is from within PowerShell?

It can be done in WMI and PowerShell has cmdlets for accessing WMI. It’s not really PowerShell’s fault that it doesn’t natively display the StartMode property though. You see, PowerShell runs on the .Net Framework and the Get-Service cmdlet returns the same properties as the underlying .Net Framework class which that cmdlet uses:

2014-04-13_20-00-49

The previous results should look familiar (they’re the same results as Get-Service).

We could use Get-CimInstance or Get-WMIObject to access WMI, specifically the Win32_Service class to see the StartMode property and its value for the BITS service:

2014-04-13_20-04-32

As you can see in the previous command, the StartMode for the BITS service is Manual.

I’m now going to change the StartMode for the BITS service to Automatic:

2014-04-13_20-07-16

No error, but did the previous command do anything? You can make the previous command return the service that was changed by adding the -PassThru parameter:

2014-04-13_20-09-23

I’ll now double check to make sure the StartMode was indeed changed and this time I’ll use the Get-CimInstance cmdlet which was introduced in PowerShell version 3 and can also be used to access WMI:

2014-04-13_20-12-38

µ

About Mike Robbins

PowerShell Enthusiast | IT Pro | Winner of the Advanced Category in the 2013 PowerShell Scripting Games | Author of Chapter 6 in the PowerShell Deep Dives Book.
This entry was posted in PowerShell and tagged . Bookmark the permalink.

One Response to Checking and Setting the Start Mode of a Windows Service with PowerShell

  1. Graeme says:

    So, I think that you need to re-write this using a DSC module to disable all the services that you specifically require so that you don’t have to do it this way…

    Challenge.

Leave a Reply