Windows PowerShell Set-Service Cmdlet

Windows PowerShell  Set-Service

One problem controlling Windows services with PowerShell is that the Startup Type maybe ‘Disabled’.  The benefit of changing the startupType property to ‘Manual’, that you can then start the service.

In order to get a grounding in the PowerShell syntax associated with this ‘Service’ family of commands, I suggest that you begin with my Get-Service page.

PowerShell’s Set-Service Topics

 ♣

Our Mission

Our mission is to start one (or more) of your operating system’s services.  However, the result of my preliminary experiment reveals that it’s not possible to start a service whose start-up type is currently set to ‘Disabled’.  Good news, a walk-through with the Services GUI reveals that if you switch a service from Disabled to Manual, then you can start it.  Indeed, this is a reminder that anything you can do in GUI, you can do with a PowerShell script.

PowerShell Pre-requisites and Checklist

In the case of Windows 7 and later, you don’t need to download any extra files, just: ‘Add Feature’ –> Windows PowerShell.  However, for older operating systems, there are different versions of PowerShell for XP, Windows Server 2003 and Vista.  For such legacy systems only, you need to download PowerShell from Microsoft’s site.

Once you have installed PowerShell 2.0 or later, I recommend choosing the ISE (Integrated Scripting Engine) version, it will save buying a text editor.

Preliminary Script to Check Startup Type For Windows Services

Before we change the Startup Type, let us digress and get a list of disabled services so that we can choose one for our experiment.

# PowerShell script to list disabled services
Clear-Host
Get-WmiObject Win32_Service |
Where-Object {$_.StartMode -eq ‘Disabled’}

Learning Points

Note 1: You could append this command in order to format the list more clearly
| Format-Table Name, StartMode -auto

Note 2: This script uses Get-WmiObject to query the StartMode or Startup Type property.

Note 3: For further experimentation I have select the PLA service (Performance Logs and Alerts), you may need to choose a different service for your task.

Guy Recommends:  A Free Trial of the Network Performance Monitor (NPM)Review of Orion NPM v11.5 v11.5

SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network.  This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.

What I like best is the way NPM suggests solutions to network problems.  Its also has the ability to monitor the health of individual VMware virtual machines.  If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.

Download a free trial of Solarwinds’ Network Performance Monitor

Set A Named Service to Manual

Windows 7 and Server 2008 do not have an Alerter service, this is why I now use PLA (Performance Logs and Alerts) to test PowerShell’s Set-Service.

# PowerShell cmdlet to set a named service to manual
$ServiceName = "PLA"
Set-Service $ServiceName -startupType manual

Simple Script to Start the Service

My idea here is to capitalize on Set-Service and actually start the PLA service.

# PowerShell set, then start a service
Clear-Host
Set-Service PLA -startupType manual
Start-Service PLA

Note 4: In this example I have just named the service and not bothered with any variables.

Note 5: In other scripts you may want to change the Startup Type to Automatic.  Once again, I recommend looking at the Service.msc snap-In while you script with PowerShell.

(Re) Setting Service To Disabled

Preliminary script to reset the service back to disabled.

# To reset PLA service back to default
Clear-Host
Stop-Service PLA
Set-Service PLA -startupType disabled

A: Inferior Script To Set a Service to Manual, Then Start It

# PowerShell set, then start a service
Clear-Host
$PLA = Get-Service PLA
"PLA status is " + $PLA.status
Set-Service PLA -startupType manual
Start-Service PLA
$PLA = Get-Service PLA
"PLA status is now " + $PLA.status

Expected Outcome

PLA status is Stopped
PLA status now is Running

B: Superior Script To Set a Service to Manual, Then Start It

The script below is a triumph for researching parameters with Get-Help Set-Service.  This reveals is a parameter called status; if we append a value of ‘Running’, then the service will start.  My point is that the above script is more cumbersome because it calls for a second cmdlet, which is not necessary.

# PowerShell Set-Service -status example.
Clear-Host
$PLA = Get-Service PLA
"PLA status is " + $PLA.status
Set-Service -name PLA -startupType manual -status running
$PLA = Get-Service PLA
"PLA status is now " + $PLA.status

Note 6: If you put the service directly after the Set-Service, then the -name parameter is assumed, however, here I have explicitly added the parameter.

Note 7: For a production script you could simplify to this one line:

Set-Service -name PLA -startupType manual -status running

SolarWinds Response Time Viewer for WiresharkGuy Recommends: Response Time Viewer for Wireshark

Here is a free tool to troubleshoot network connection and latency problems.  Key concept: this is a free tool from SolarWinds that analyzes network packets captured by Wireshark (also a free tool).

When you inspect the data in the Response Time Dashboard, if you hover over an application such as Teredo or TCP, then you get an orange box showing a breakdown of network and application response times, note the 'Peak value' in addition to the 'Average'.

Download your free trial of SolarWinds Response Time Viewer for Wireshark

Remoting with Services

One aspect of remoting in PowerShell v 2.0 is simply to append -computerName xyz to the command that you ran on the local machine.  For further research try:

Clear-Host
Get-Command| where { $_.parameters.keys -Contains "ComputerName"}

Surprise!  Get-Service is amongst the cmdlets that support remoting, but set, start and Restart-Service are not on the list.  More bad news, stop, start and Restart-Service really don’t work on network machines.  Thus you have to employ different techniques such as Get-WmiObject and InvokeMethod.  Alternatively, you could enter-PSSession and then run Restart-Service as if you were on the local machine.  However, to get that working you have to first install and setup WinRM

Research Set-Service Parameters

# Extra parameters for PowerShell’s Set-Service
Clear-Host
Get-Help Set-Service -full

Note 7:  In addition to the -status parameter used above, you can also use -DisplayName instead of name.  This is useful for services such as Spooler, with its confusing DisplayName of ‘Print Spooler’

The Service Family (Each member has a different verb)

Get-Service:  Useful for listing the services
Set-Service:  Crucial parameter -startuptype
Start-Service:  The verb ‘start’ says it all
Stop-Service:  Handy for scripts which prevent unwanted services running e.g. Telnet
Restart-Service:  A nice touch by the creator’s of PowerShell; this cmdlet removes the need to explicitly stop then start the service.

See also a function I created called Get-ServiceStatus »

Summary of PowerShell’s Set-Service Cmdlet

Set-Service is particularly useful for dealing with services that have been disabled.  In this case Start or Restart-Service fail, but Set-Service works thanks to the -startupType parameter.  Researching cmdlets for with Get-Help is always useful, and in this case guides us to start services thanks to the -status parameter.

If you like this page then please share it with your friends

 


See more PowerShell examples of process and service

PowerShell Home   • Get-Process   • Stop-Process   • PowerShell Start-Process   • Set-Service

Get-Service   • Start-Service   • Stop-Service   • Restart-Service   • Free WMI Monitor

PowerShell Service Account   • Get-WmiObject win32_service   • Windows PowerShell

Please email me if you have a better example script. Also please report any factual mistakes, grammatical errors or broken links, I will be happy to correct the fault.