Windows PowerShell Set-Service Cmdlet
Windows PowerShell Set-Service
One problem controlling 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 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
Note 1: You could append this
command to format the list more clearly
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.
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.
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
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
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
# PowerShell set, then start a service
PLA status is Stopped
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.
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
Here is an utility where you can review firewall settings such as access control lists (ACL), or troubleshoot problems with network address translation (NAT).
Other reasons to download this SolarWinds Firewall Browser include managing requests to change your firewall settings, testing firewall rules before you go live, and querying settings with the browser's powerful search options.
Guy recommends that you download a copy of the SolarWinds free Firewall Browser.
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:
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
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
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
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.