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.
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 Server
2008, you don't need to download any extra files, just 'Add Feature' Windows PowerShell.
However, for older operating systems, installing can be confusing because
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, I recommend choosing the
ISE (Integrated Scripting Engine) version, it
will save you buying a text editor.
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 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.
Encouraging computers to sleep when not in use is a great idea -
until you are away from your desk and need a file on that remote sleeping machine!
Wake-On-LAN really will save you that long walk to awaken a hibernating
machine; however my reason for encouraging you to download this utility is
just because it's so much fun sending those 'Magic Packets'. As Wake-On-LAN (WOL) is free, see
if I am right, and you get a kick from arousing those sleeping machines.
WOL also has business uses for example, wakening machines so that they can have
their patches applied.
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
# 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
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:
Import users from a spreadsheet. Just provide a list of the
users with their fields in the
top row, and save as .csv file. Then launch this FREE utility and match
your fields with AD's
attributes, click to import the users. Optionally, you can
provide the name of the OU where the new accounts will be born.
There are also two bonus tools in this free download, and all 3 have been approved by Microsoft:
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.
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.
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft operating systems.
Fortunately, Solarwinds
have created the
Free WMI Monitor so that you can actually see and understand these gems of
performance information. Take the guess work out of which
WMI counters to use for applications like Microsoft Active Directory,
SQL or Exchange Server.