PowerShell


Windows PowerShell Introduction

Windows PowerShell Introduction PowerShell, Microsoft's Scripting Shell - Getting started for Administrators

My goal on this page is to persuade Windows administrators that Microsoft's PowerShell is a friendly scripting language, which is well worth learning.

Using my examples and learning points, you will soon be able to interrogate the server's operating system.  So get started, by downloading and installing Windows PowerShell, together with .Net Framework.

Introduction to Windows PowerShell Topics

Who is Windows PowerShell designed for?

What impresses me about PowerShell is that even as an amateur, you can get started easily and you will soon be creating interesting scripts. The hidden message is that PowerShell works at many levels, moreover, you can quickly rise through these levels. As your journey proceeds, PowerShell teaches you about scripting theory, and scripting theory teaches you which constructions to look for in PowerShell.

The vision behind PowerShell is to provide cmdlets (scripts) which automate repetitive tasks.  There is a feeling, at least in Exchange 2003/7, that people cannot find settings because they are hidden underneath so many levels of sub-menus.  What I find is that it soon becomes as fast to issue a few commands in the Microsoft Shell, as to configure the same settings through a GUI.

Admins who have always wanted a command line shell for interactive scripting are going to enjoy PowerShell.  The most amazing feature of PowerShell is that it lets you think in previous language, while you figure out its native ways of operating. Using the built-in Aliases, or creating new Aliases is the key to this transition.

Your first seven steps to learning PowerShell

  1. Download PowerShell (Get an OS specific version)
  2. Begin with familiar old dos commands: dir, CD or Ipconfig (Just to get comfortable)
  3. Make a collection of verb-Noun pairs, e.g. set-Executionpolicy and get-Childitem
  4. Call for help with get-Help wmiobject and master get-Member
  5. Create lots of scripts or Cmdlets
  6. Deploy the Pipe symbol | (Join commands | filter)
  7. Master $_.  This construction means the current object in the pipeline.
    For example: where | {$_.name -contains "Microsoft"}

Getting started with PowerShell

No other scripting language is so easy for the newcomer, yet offers so much sophistication for the experienced script writer.  One reason that you can start learning PowerShell NOW is you can still use all your old commands such as: cd, dir, and even Ipconfig.  My vision is that little by little, you can progress from commands you already know, e.g. dir, to PowerShell's get-ChildItem.

Now I don't pretend that it's easy to become an expert at PowerShell, I merely want to make the point that it's easy to begin that scripting journey.  Just as in golf, most people can get the ball airborne with a sand wedge, but only a professional can hit a ball out of the rough, clear a water hazard, then hold the ball on a down-slope.  PowerShell is like golf in that anyone can play, but few will have the effortless control of a professional.  To stretch my analogy to the limit, just as we can play along with our favourite golfers from the comfort of our armchairs, so we can emulate PowerShell's experts by copying and pasting their code.

To get you started, here are five examples to type at the PS prompt type:

  1. get-command
  2. get-command *event
  3. get-Eventlog system
  4. get-Eventlog system -newest 100
  5. get-Eventlog system -newest 100 | where {$_.eventid -eq 20}

See more on getting started with PowerShell commands

My Challenge - Start your PowerShell journey

My aim is to get you writing your own PowerShell cmdlets.  Meanwhile, here is my challenge to you: in ten minutes you will be able to perform all the tasks that you currently achieve at the CMD prompt.  Moreover, at the end of half an hour you will be issuing PowerShell commands that are just not possible with cmd.exe.

My assumption is that you have successfully installed PowerShell and .NET Framework 2.0 (or 3.0).  Begin by clicking Start, Run, and type powershell in the dialog box; alternatively, create a short cut to powershell in the Quick Launch area of the Taskbar.

Try out a few PowerShell commands

Normally, when people move to another scripting language their old trusty commands don't work in the new shell.  Well, you will be pleasantly surprised that when you migrate from cmd.exe, old favorites such as cd and dir are available thanks to PowerShell's Aliases.  In a short time you will adapt to the new ways of doing old tasks; for example, set-location is the equivalent of cd, and get-Childitem achieves the same result as dir.  In addition, PowerShell allows you to run all the native Windows executables such as Ipconfig, NetSh and NetDiag.

Thanks to this built-in library of Aliases, those coming from UNIX, perl or .NET can also make an easy transition to PowerShell.  I don't want you to underestimate the body of knowledge required to be productive in PowerShell, I just want to emphasise that it's easy to take that first step and build on familiar commands from your existing scripting language.

Principle 1:  At the heart of every PowerShell command is a verb-noun pair.  You start with verb, add a hyphen and then finish with a noun.  For example, set-location, which is the equivalent of cd (change directory) in DOS.  If you find exceptions that only require the noun, this is because PowerShell's intelligence adds the default verb 'get'.  For example if you type, 'childitem', PowerShell assumes you mean get-Childitem.  More good news, advanced scripters can create their own aliases. 

PowerShell Introduction - Simple Examples

I realize that these are Windows rather than Exchange 2007 examples but I want to get you up and running:
Preamble, Start, Run, PowerShell.  In the shell type:

get-Process
or
get-Eventlog system

Principle 2:  Create lots of scripts or cmdlets.  What I mean is save your commands in a text file with a .ps1 extension, for example ProcessAll.ps1.  Moreover, save that file into PowerShell's working directory.  Now you are ready to execute these cmdlets from within this Microsoft Shell, for example:  .\ProcessAll.  Your two key characters are the dot and the backslash.  The secret is get into the rhythm: dot, backslash then filename.  There is no need to add the .ps1 extension, also avoid spaces.

.\cmdlet_filename

The Knack

The knack is to store all your cmdlets in a folder which you can easily reach from the PowerShell prompt.  At the risk of teaching my grandfather to suck eggs, I suggest that you adopt one of these strategies.

a) Save all cmdlets to the default PowerShell directory.
b) Once PowerShell launches, immediately change directory to the folder containing your cmdlets
    cd C:\ cmdlets.
c) There is an advanced technique to re-program the default PowerShell directory to a folder of your choice.

If all else fails just type the complete path:
C:\Documents and Settings\GUYT\Documents\PSConfiguration\ProcessAll
(Or D:\scripts\ProcessAll)

Powershell tip - use tab for auto-completion  Auto-completion:
To save typing, and reduce the risk of making a spelling mistake, invoke auto-completion by pressing tab.

Auto-completion examples
get-ev [tab] auto-completes to --> get-Eventlog
set-lo [tab] auto-completes to --> set-location

Example of a PowerShell Cmdlet

# This cmdlet generates a report about memory in active processes
# Memory usage (>100000000)"
Write-Host Report generated at (get-date)
Write-Host ""
get-Process | where-object { $_.VirtualMemorySize -gt 100000000 }

See more on getting started with PowerShell

  ˚

PowerShell's Built-in Help

PowerShell's help is clear, concise and full of useful examples.  Put your faith in Powershell's help.  Cast aside negative experiences of other help files.  Overcome your pride and ask for help.  All that you need to query the vast help information store, is either precede the item with 'help', for example, help get-Member; or else append -? for example, get-Process -? (hyphen question mark).  The executable powershell.exe even has its own help, try this at the PS prompt, type: powershell -?

Another useful sister cmdlet is: get-command, which lists the built-in cmdlets.  Better still, try the * wildcard:

get-command *service.

Summary of PowerShell Introduction

PowerShell is Microsoft's new scripting language. It is designed to supersede VBScript and eventually to replace the 'Dos box'. What makes it easy to get started is that all the old DOS commands still work at the PowerShell command line.

Trust me; it really is easy to get started with PowerShell verb-noun commands. Therefore make sure that you download a version for your operating system, and start learning from the PowerShell script examples that I will supply on other pages.

See also these PowerShell topics

PowerShell Home  • Introduction  • Dreams  • 3 Key Commands  • Cmdlet scripts  • Real life tasks

Please write in if you see errors of any kind.  Please report any factual mistakes, grammatical errors or broken links, I will be happy to not only to correct the fault, but also to give you credit.

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.

 *


Google

WebComputerperformance.co.uk

 

Home Copyright © 1999-2008 Computer Performance LTD All rights reserved

Please report a broken link, or an error.