This page of
PowerShell examples concentrates on Windows services. While 'Get'
is PowerShell's default verb, this page alerts you to additional verbs
such as stop, start and restart.
Topics for PowerShell Service
Examples Finding Files
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.
# Simple PowerShell example to list Windows Services Get-Service
Note 1: One of the first things that you look for
with Windows services is which are running and which are stopped. This
leads us to a employing the 'Where' clause to filter the output so that we
get a list of services that are running.
# PowerShell example to list Windows Services running Clear-Host
Get-Service | Where-Object {$_.status -eq 'Running' }
Note 2: This refinement introduces one of the
key PowerShell constructions, namely the (|) pipe, where the output
of Get-Service becomes the input of Where-Object.
Note 3: PowerShell introduces
comparison operators
with a minus sign (-) thus -eq, -like, or -match.
Guy Recommends: SolarWinds' Free Bulk Import Tool
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:
It may have struck you how PowerShell's building block is the cmdlet,
furthermore it always uses verb-noun pairs. The examples on this page
all feature the noun Service, note as with all PowerShell nouns Service is
singular. Get is the most common PowerShell noun, and is the default
should you call for a noun without a verb, such as
# PowerShell example illustrating why get is the default verb Service
Where-Object {$_.status -eq 'Stopped' }
Time to investigate more PowerShell nouns.
# PowerShell Verbs for Service Clear-Host Get-Command -noun Service
If as a result of Example 1 you find a service that is stopped that
should be running, then you could call for PowerShell Start-Service to
reverse the status. However, the most common scenario is where you
want to restart a service such as the spooler because it has hung or has
stopped working.
# PowerShell script to restart the spooler Service
Clear-Host Restart-Service Spooler
Note 4: The above script does not return
any result or confirmation, so you may wish to add a command to
check the status of the spooler service.
See more Restart-Service
examples.
# PowerShell script to restart the spooler Service Clear-Host
$SrvName = 'Spooler' Restart-Service $SrvName Get-Service $SrvName
Note 5: This script also applies knowledge from
example 1 and appends the Get-Service command to check the status of the
service represented by the variable $SrvName.
Guy Recommends: SolarWinds' Free Bulk Import Tool
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:
Further Research for PowerShell in General and Get-Process in Particular
Get-Help
If you check the parameters of any PowerShell cmdlets it will increase
your range of skills, and possibly solve a nagging problem. Thus
get into the habit of running Get-Help in front of any new cmdlet, thus:
# PowerShell Research Cmdlet Parameters Clear-Host Get-Help
Get-Service -full
Note 6: With Get-Help always append the -full
switch and thus see examples. One bonus of this research is
the realization, or reminder that we could use -computer to discover
what's happening with services on another machine.
Get-Member
While not all cmdlets benefit from Get-Member's ability to research
properties, Get-Service is a classic case where selecting the properties
in the output can dramatically improve your script.
Note 7: Two trivial points: unlike help, you
do need that pipe (|). Secondly remember the sequencing, in
this example it's cmdlet first, followed by the pipe and then
Get-Member.
Note 8: I have focussed this command by appending
the -memberType parameter and asking it to filter on properties
(and exclude methods).
Guy Recommends: SolarWinds' Free Bulk Import Tool
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:
Following the research from Get-Member, I have decided to investigate the
difference between ServiceName and DisplayName because this often causes
confusion when scripting services.
Note 8: Once again note how PowerShell pipes
the output of one command so that it becomes the input for the next
instruction.
Note 9: To double check what I mean launch the
Windows services.msc and compare the Name and Description with PowerShell's
output ServiceName and DisplayName.
The purpose of this page is to control Windows Services through
PowerShell scripts. Thanks to Get-Command -noun we can investigate
alternatives to 'Get', for example Restart-Service. Furthermore,
comparing the Windows Services GUI with Get-Service properties explains
the difference between a service's name and its display description.
If you like this page then please share it with your friends
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.