Guy recommends :
Free SolarWinds
VM Console

Solarwinds VM Console Free Download

Find out which of your VMs are a waste of space and which VMs need more resources.



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 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.

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:  SolarWinds' Free Bulk Import ToolFree Download of 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 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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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:  SolarWinds' Free Bulk Import ToolFree Download of 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 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:

  1. Bulk-import new users into Active Directory.
  2. Seek and zap unwanted user accounts.
  3. Find inactive computers.

Download your FREE bulk import tool.

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   • 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.

Download my ebook:Getting Started with PowerShell
Getting Started with PowerShell - only $9.25

You get 36 topics organized into these 3 sections:
   1) Getting Started
   2) Real-life tasks
   3) Examples of Syntax.

In addition to the ebook, you get a PDF version of this  Introduction to PowerShell ebook  It runs to 120 pages of A4.

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor and It's Free!Solarwinds WMI Monitor

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.

Download your free copy of WMI Monitor

Author: Guy Thomas Copyright © 1999-2012 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: