PowerShell


Windows PowerShell for Vista Introduction

Windows PowerShell for Vista Introduction

My mission is to help install Microsoft's PowerShell on Vista.  Once you have installed PowerShell, the first task is to set the script execution policy:

  1. help set-executionpolicy
  2. set-executionpolicy remotesigned
  3. get-executionpolicy

How to launch PowerShell in Vista

Introduction to Windows PowerShell Topics

 ♣

7 Things to get you started with PowerShell

  1. Where to Download PowerShell (Get OS specific version)
  2. Begin with the old dos commands (dir, CD, Ipconfig) - Just to get comfortable
  3. Look for the Verb-noun Pair (Set-executionpolicy)
  4. Call for help (get-help wmiobject)
  5. Create Scripts (Cmdlets)
  6. Deploy Pipe symbol | (Join commands | filter)
  7. Master $_.  It means the current object in the pipeline. (Where {$_.name -contains "Microsoft})

How to Launch PowerShell in Vista

  • Download the correct version of PowerShell from Microsoft's site.  Naturally for Vista, choose the Vista x86 or x64 version and avoid XP or Windows Server 2003 versions.
  • Install the PowerShell file with the .msu extension, mine was called:
    Windows6.0-KB928439-x86.msu.
  • Find the PowerShell file!  I tried Search 'PowerShell'.  This revealed a shortcut to the executable in this folder:
     Start Menu -->  Programs --> Windows PowerShell.
  • The underlying executable, called PowerShell.exe was found in:
     %Windir%\\System32\WindowsPowerShell\v1.0 (note the final sub-directory).
  • With PowerShell, my favorite thing to do, is to run cmdlets. These are nothing but PowerShell commands in a text file, but saved with .ps1 extension. Vista Powershell shortcut
  • Problem 1: By default, you cannot 'run' or execute .ps1 files.  This script restriction for security reasons.
    Solution 1: You can change the ExecutionPolicy from restricted to RemoteSigned (best) or Unrestricted.
  • Problem 2: If you run PowerShell normally, you don't have the permission (rights) to change the ExecutionPolicy.
    Solution 2: Run PowerShell as an Administrator.  It's simplest to accept this rather than argue - that you are already logged on as THE ADMINISTRATOR.  What I do to get through this security restriction is:
    a) Create a shortcut to PowerShell.
    b) Right-click the shortcut, select Properties, Shortcut (tab) and then Advanced (button)
    c) Tick the box: Run as Administrator.
  • Launch PowerShell as Administrator, run this command:
    Set-ExecutionPolicy remotesigned  (note there is no minus sign in front of remotesigned)
  • Now you can run cmdlets, which are those instruction files with a ps1 file extension.

Feel the control and agility of PowerShell

In this section of my website I am going to focus on getting you started with Microsoft's PowerShell.  Before I give you more examples, I want to say to developers and programmers that PowerShell has all the control and agility of languages such as perl, ruby and python.  One clue of PowerShell's object nature is that you first need to install .NET Framework 2.0, otherwise PowerShell cannot work.  Above all else, PowerShell gives administrators the power and versatility of the UNIX Bash command line.

My most difficult concept to convey is an act of faith.  Trust me that PowerShell will deliver the goods and exceed your expectations.  Here is an example of what PowerShell could do; imagine this scenario: 'One of my servers is running slower than the rest, can I check the version numbers of all the processes?'.  You are unlikely to find a built-in tool to answer this question, however, if you are expert in PowerShell you will be able to build cmdlets to answer this and similar operating system questions.

Programmers and developers will soon create scripts or cmdlets, yet my task is to persuade those with just server administration skills that they too can create code to interrogate the operating system.  Naturally, Admins will use the time-honoured tradition of copying other people's scripts, indeed one of my goals is to create a bank of such cmdlets.  My greatest challenge is to provide a middle way, to persuade Admins that they can decipher the commands and easily modify the code to suit their own situations.

Guy Recommends: SolarWinds Engineer's Toolset v10Engineer's Toolset v10

The Engineer's Toolset v10 provides a comprehensive console of utilities for troubleshooting computer problems.  Guy says it helps me monitor what's occurring on the network, and the tools teaches me more about how the system literally operates.

There are so many good gadgets, it's like having free rein of a sweetshop. Thankfully the utilities are displayed logically: monitoring, discovery, diagnostic, and Cisco tools.  Download your copy of the Engineer's Toolset v 10

My Challenge - To get you started.

My goal is to get you started with PowerShell.  My challenge is that in thirty minutes you will be able to perform all tasks that you currently achieve at the CMD prompt, and start issuing new PowerShell commands that are just not possible with CMD.EXE.

Assumption, you have successfully installed PowerShell and .NET Framework 2.0.  Begin by clicking Start, Run and type powershell, alternatively, create a short cut to powershell in the Quick Launch area.

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, if you migrate from cmd.exe to PowerShell, then cd and dir are available thanks to Aliases.  In a short time, you adapt to the new ways of doing the same things.  For example, set-location is the equivalent of cd, and get-Childitem the Alias for dir.  In addition, PowerShell allows you to run all the native Windows executables such as Ipconfig, Tracert and NetDiag.

The miracle of PowerShell is that thanks to Aliases, those coming from UNIX, perl or .NET can make a similar transition but employing their much more high powered commands.

Principle 1:  The heart of PowerShell is always a verb-noun pair.  You start with verb add a hyphen then finish with a noun.  For example, set-location is the equivalent to cd (change direcory) in DOS.  Incidentally, the reason cd works in PowerShell is because it has a built in Alias called cd which maps to set-location.  Good news, you can create your 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 dark blue shell type:

get-Process
or
get-Eventlog system

Principle 2:  Create lots of scripts or cmdlets.  Save your text commands in a text file with a .ps1 extension, for example ProcessAll.ps1.  Moreover, save that file into PowerShell's working directory.  Then execute these cmdlets from within the 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.

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  To save typing and risk making a spelling mistake use the Tab key to invoke auto-completion.

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
#

"Report generated at " + (get-date)
"" # insert blank line

"High memory usage (>100000000)"

get-Process | where-object { $_.VirtualMemorySize -gt 100000000 }

 

Guy Recommends: SolarWinds LANSurveyorSolarwinds LANSurveyor

LANSurveyor will produce a neat diagram of your network topology.  But that's just the start; LANSurveyor can create an inventory of the hardware and software of your machines and network devices.  Other neat features include dynamic update for when you add new devices to your network.  I also love the ability to export the diagrams to Microsoft Visio.

Finally, Guy bets that if you take a free trial of LANSurveyor then you will find a device on your network that you had forgotten about, or someone else installed without you realizing!

Download a Free Trial of LANSurveyor

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

Who is Windows PowerShell designed for?

What impresses me about PowerShell is that even as an amateur, I can get going and create interesting scripts with ease.  The hidden message is that PowerShell works at many levels, moreover, you can easily rise through the levels.  In addition PowerShell teaches you about scripting theory, and scripting theory teaches you what to look for in PowerShell.

The vision of PowerShell is to provide cmdlets (scripts) which automate repetitive tasks.  There is a feeling, at least in Exchange 2003, that people cannot find settings because they are hidden underneath so many levels of sub-menus.  What I find is that with both Exchange and WMI cmdlets 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 want a command line shell for interactive scripting will 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.  Creating new Aliases or using the built-in Aliases is the key to this transition.

  ˚

PowerShell Overview

The secret of PowerShell is to put commands in a file (cmdlet) and then forget them until the next time you need their automation.  With PowerShell you can manipulate text objects in an advanced manner.  You also get access to data stores which can be accessed as namespaces, for example, WMI and the Registry.

Consistency is a watchword, learn a verb in one area, then apply it to other PowerShell objects. For example, once we learn the verb 'Stop', there will never be a need to deviate, we don't suddenly change to 'kill' or 'terminate'

PowerShell supports COM, WMI, ADSI .NET XML.  Also unified tools from C++ C# and perl.  Naturally it supports Sub Routines, Constraints and utilities, for example: foreach, group, select, sort and where.

It is claimed that MMC v 3 and the Exchange 2007 System Manager will have the ability to auto-generate PowerShell scripts.  The catch phrase is: 'Figure out, then manipulate'.

After I got PowerShell installed and when I had mastered a few simple commands, I listened to two 90 minute Microsoft Webcasts of PowerShell.  If I had listened to the presentation first, I would not have gone any further, I would have thought, 'PowerShell is too difficult for non-programmers like you Guy'.  So my message is suck it and see, just try some of my examples, you will be amazed how easy it is get going with PowerShell.

However, there was a very important hidden message in that presentation, it was saying to me, 'This is a seriously powerful scripting language'.  The speaker explained how PowerShell could do stuff that people now do in UNIX, perl, C++ or Visual Basic.  One day I will return that presentation because there is so much for me to learn about scripting principles in general and objects in particular.

Summary of PowerShell Introduction

With Microsoft's PowerShell the underlying philosophy is to provide powerful scripts to automate occasional tasks. Furthermore, PowerShell is designed to make it easy for people to transition from other languages.  Trust me, it really is easy to get started with PowerShell commands.

See more Microsoft PowerShell tutorials

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

Remote PowerShell  • -Online  • WinRm and WSMan  • Invoke-Command  • Jobs  -asJob   • PowerShell

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

Web  This website

Review of Orion NPMGuy Recommends: Orion's NPM - Network Performance Monitor

Orion's performance monitor is designed for detecting network outages. A network-centric view make it easy to see what's working, and what needs your attention.

This utility guides you through troubleshooting by indicating whether the root cause is faulty equipment or resource overload.

Download a free trial of the Network Performance Monitor

 

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

Please report a broken link, or an error.