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.
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.
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: WMI Monitor and It's Free!
Windows Management Instrumentation (WMI) is one of the hidden
treasures of Microsoft 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.
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.
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: Solarwinds' Log & Event Management Tool
LEM will alert you to problems such as when a key
application on a particular server is unavailable. It can also
detect when services have stopped, or if there is a
network latency problem. Perhaps this log and event management
tool's most interesting ability is to take corrective action, for
example by
restarting services, or isolating the source of a maleware attack.
Yet perhaps the killer reason why people
use LEM is for its
compliance capability, with a little help from you, it will ensure that your organization complies with industry
standards such as CISP or FERPA. LEM is a really smart
application that can make correlations between data in different logs,
then use its built-in logic to take corrective action, to restart services,
or thwart potential security breaches.
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.
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.
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
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 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.