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.
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.
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 I want to introduce you to PowerShell 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:
Get-Command
Get-Command get*
Get-Eventlog system
Get-Eventlog system -newest 100
Get-Eventlog system -newest 100 |
where {$_.eventid -eq 20}
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.
My aim in this introduction to PowerShell 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, 3.0
or 4). 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.
See more on PowerShell's version
checker.
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
Guy Recommends: A Free Trial of the Network Performance Monitor
(NPM)
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.
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)
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 }
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.
If you like this page then please share it with your friends
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.
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.