Setting a Startup type of Service with Powershell

Recently I needed to change the Start Type of a Windows Service from Powershell. Course i need to know which ones are disable out of my Filtered set of Services. 

CmdLet ‘Get-Service’ 

it produces an Object of System.ServiceProcess.ServiceController Object which doesn’t have Startuptype parameter. This was primary problem on how to query the services which have startup type as disabled, and then setting the startup type of the services to whatever I intend to set.


Doing some binging i also saw some P/Invoke based C# code which does need a thought to code and compile. I wanted the solution to be simple and which works on most of the Windows systems without the need to write code, compile and port.

  • Powershell offers a easy interface to CIM Instances for getting Service Objects out of CIM implementation. The command below just returns the service objects which are disabled 

[code language=”powershell”]
Get-CimInstance Win32_Service -filter "startmode=’disabled’" | get-member
Output Object type is "Microsoft.Management.Infrastructure.CimInstance#root/cimv2/Win32_Service"
[/code]

  • This is very much true with regards to WMI as well, though the Outputobject is a bit different but has most of the parameters/ methods as CIMInstance object.

[code language=”powershell”]
Get-WmiObject Win32_Service -filter "startmode=’disabled’"  | get-member
Output Object type is System.Management.ManagementObject#root\cimv2\Win32_Service
[/code]

Pipe-lining to Set-service 

CmdLet Set-service has 2 type of implementations which take pipeline inputs as either “System.ServiceProcess.ServiceController” or System.String which -Name value. 

       – -InputObject <ServiceController> – (ByValue) 

       – -Name <String> – (ByValue, ByPropertyName)

Clubbing it together .

Looking at the Output-object of Get-CIMInstance/Get-WmiObject for win32_service revealed that ‘Name’ property has the value of actual name of the Service fitting in my filter criteria. The Name parameter matches with the ByPropertyName based -Name pipeline input to the Set-Service CmdLet .

Hence, below commands work a good way.

1. List of Services having Disabled as Startup type. Remember the property is startmode rather than Startuptype .

       a. Get-CimInstance Way . Both of the below commands produce same output :  –

[code language=”powershell”]
Get-CimInstance Win32_service -filter &quot;startmode=’disabled’ and Name like ‘%SQL%’&quot;
Get-CimInstance Win32_Service | where {$_.startmode -eq ‘Disabled’ -and $_.name -like ‘*sql*’}
ProcessId            Name             StartMode            State                Status              ExitCode
———            —-             ———            —–                ——              ——–
0                    SQLBrowser       Disabled             Stopped              OK                  1077

0                    SQLAgent$SHAR… Disabled             Stopped              OK                  1077
[/code]
      b. WMI implementation of above commands below

[code language=”powershell”]
Get-WmiObject Win32_service -filter &quot;startmode=’disabled’ and Name like ‘%SQL%’&quot;
Get-WmiObject Win32_Service | where {$_.startmode -eq ‘Disabled’ -and $_.name -like’*sql*’}
[/code]

2. Setting the Service startup type now

    Below command sets this up perfectly. Watch out for -whatif output before running this to see what you are doing.

[code language=”powershell”]
Get-CimInstance win32_service -filter &quot;startmode=’disabled’ and Name like ‘%SQL%’&quot; | set-service -StartupType Disabled -whatif
What if: Performing the operation &quot;Set-Service&quot; on target &quot;SQL Server Browser (SQLBrowser)&quot;.
What if: Performing the operation &quot;Set-Service&quot; on target &quot;SQL Server Agent (SHAREPOINT) (SQLAgent$SHAREPOINT)&quot;.

Get-CimInstance win32_service -filter &quot;startmode=’disabled’ and Name like ‘%SQL%’&quot; | set-service -StartupType Manual
[/code]

I hope this helps you.