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:

1Get-Service -Name BITS

2014-04-13_19-14-48.png

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:

1Get-Service -Name BITS | Select-Object -Property *

2014-04-13_19-21-23.png

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:

1Add-Type -AssemblyName 'System.ServiceProcess'
2[System.ServiceProcess.ServiceController]::GetServices() |
3Where-Object Name -eq BITS |
4Select-Object -Property *

2014-04-13_20-00-49.png

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:

1Get-WmiObject -Class Win32_Service -Filter "Name = 'BITS'" |
2Select-Object -Property *

2014-04-13_20-04-32.png

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:

1Set-Service -Name BITS -StartupType Automatic

2014-04-13_20-07-16.png

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:

1Set-Service -Name BITS -StartupType Automatic -PassThru

2014-04-13_20-09-23.png

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:

1Get-CimInstance -ClassName Win32_Service -Filter "Name = 'BITS'" |
2Select-Object -Property Name, StartMode

2014-04-13_20-12-38.png

µ