Windows PowerShell Examples

Examples to Illustrate Windows PowerShell Basics

While PowerShell has all the complexities of leading scripting language, it is particularly easy to pick up the basics.  To see how friendly PowerShell is for the novice try my simple examples on this page.

Basic PowerShell 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: List Log Files in the Windows Folder

While Example 4 will list all the files in the Windows directory and then sort them into order of the most occurrences of the word 'Error', let us begin with a basic example.

# Basic PowerShell example to find Windows log files
Get-Childitem C:\Windows\*.log

Note 1:  The first point to remember about PowerShell is that the cmdlet is the basic building block for more complex scripts.  In this instance note the rhythm of the verb noun pair Get-ChildItem.

Note 2: Incidentally, this simple construction (C:\Windows\*.log) is much faster than (C:\Windows\* -Include *.log).  You will see the difference when we ask -recurse to drill down to the sub-directories in Example 3.

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 2:  Get-Help Get-ChildItem

While experienced PowerShell users won't admit it, everyone sneaks a look at Get-Help when they research a new cmdlet.  My purpose in introducing Get-Help is to reveal the secret of how I learn about PowerShell.

# PowerShell Basics: Get-Help for Cmdlets
Clear-Host
Get-Help Get-ChildItem  -full

Note 3:  Actually, plain 'Help Verb-Noun' also works because 'Get' is the default verb and PowerShell assumes that what you meant, for instance try: 'Help Get-ChildItem'.  Talking of assumptions PowerShell assumes that first instruction is for the location, thus you don't need to explicitly use the -path parameter in most Get-ChildItem scripts.

Note 4:  The point of calling for help is to seek useful -parameters for your project (Some people call them switches).  In this case -recurse, -Include and possibly -force are important modifiers for the main Get-ChildItem cmdlet.

Note 5:  I don't know why you would ever call for Get-Help without appending its -full parameter.  What this does is give you examples of how apply the particular cmdlet.

Example 3:  List Log Files in Subdirectories

By getting to know the parameters you can solve problems, for example by employing -Include and -recurse in tandem you can list all the log files under the Windows folder.

# PowerShell example to find Windows log files
Clear-Host
$Directory = "C:\Windows\"
$Files = Get-Childitem $Directory -recurse -Include *.log `
-ErrorAction SilentlyContinue

Note 6:  One reason that I used the variable $Directory is that I wanted to remind you to change the value of the path before adapting this script to another PowerShell project.

Note 7:  Backtick (`) is PowerShell's word-wrap, it means continue the same command on the next line.

Note 8:  The CommonParameter '-ErrorAction SilentlyContinue'  permits Get-ChildItem to cope with log files which are in use.  To see what I mean, try the above script without the last line.

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 4: PowerShell Real-life Task -  Find Windows Log Files With 'Errors'

The scenario is that we want an overview of our Windows log files.  Time is short so we just want a list of the files which contain the most 'Errors'.  Incidentally, if you try this script for real you will be shocked at how many errors there are in your Windows operating system.

# PowerShell example to find and sort Windows log files
Clear-Host
$Directory = "C:\Windows\"
$Phrase = "Error"
$Files = Get-Childitem $Directory -recurse -Include *.log `
-ErrorAction SilentlyContinue
$Files | Select-String $Phrase -ErrorAction SilentlyContinue `
| Group-Object filename | Sort-Object count -descending

Note 9:  Compared with Example 1 this represents a huge leap in complexity.  The secret of unravelling how it bolts together the elements is to look for the (|) pipes.  The vertical bar (|) is PowerShell's signature tune, it pipes the output of the first command into the second phrase, and then output of the second phase into the third part.

In this instance, once 'Get-ChildItem' supplies its list of log files, Select-String filters this stream, then passes it along the chain for grouping and sorting.

Note 10:  See more on ErrorAction control methods.

Where Next?  More PowerShell Examples

Summary of PowerShell Basics

While PowerShell has all the complexities of leading scripting language, it is easy to get started.  The first point to remember about PowerShell is that the cmdlet is the basic building block.  The secret of success is to remember the rhythm of Verb-Noun, for example Get-Help, or Get-Childitem.  It is possibly to learn the basics just through trying my PowerShell examples.

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.

 

 *


Custom Search

Site Home

Guy Recommends: WMI Monitor for PowershellSolarwinds WMI Monitor

Windows Management Instrumentation (WMI) is most useful for PowerShell scripting.

SolarWinds have produced this Free WMI Monitor to 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-2016 Computer Performance LTD All rights reserved.

Please report a broken link, or an error to: