PowerShell Examples – Get-Service

PowerShell Examples Featuring Get-Service

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

 ♣

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.

PowerShell Example 1: Get-Service – The Basics

# 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:  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

Example 2: More Verbs For PowerShell’s Service

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

Results

# PowerShell Verbs for the Service object

Cmdlet Get-Service
Cmdlet New-Service
Cmdlet Restart-Service
Cmdlet Resume-Service
Cmdlet Set-Service
Cmdlet Start-Service
Cmdlet Stop-Service
Cmdlet Suspend-Service

Example 3: PowerShell Restart-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 ToolFree Download Solarwinds 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 and import the users.

Optionally, you can provide the name of the OU where the new accounts will be born. Download your FREE bulk import tool.

If you need more comprehensive application analysis software,
Download a free trial of SAM (Server & Application Monitor)

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.

# PowerShell Research Cmdlet Parameters
Clear-Host
Get-Service | Get-Member -memberType Property*

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: Free WMI Monitor for PowerShellSolarwinds Free WMI Monitor for PowerShell

Windows Management Instrumentation (WMI) is one of the hidden treasures of Microsoft’s operating systems.  Fortunately, SolarWinds have created a Free WMI Monitor so that you can discover these gems of performance information, and thus improve your PowerShell scripts.

Take the guess work out of which WMI counters to use when scripting the operating system, Active Directory, or Exchange Server. Give this WMI monitor a try – it’s free.

Download your free copy of WMI Monitor

Example 4: Choosing Properties for Get-Service

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.

# PowerShell Research Cmdlet Parameters
Clear-Host
Get-Service | Format-Table ServiceName, DisplayName -auto

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.

Where Next?  More PowerShell Examples

Summary of PowerShell Service Examples

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

 


See more Windows PowerShell tutorials

PowerShell Tutorials  • PowerShell Examples  • PowerShell Service  • PowerShell Loops

Process Example  • PowerShell services  • Get-Member  • PowerShell Home   • Windows PowerShell

Top 10 Commands to Try in PowerShell  • PowerShell v3   • Free CSV Import Tool

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.