Ezine 196 - Learn PowerShell 2.0
Ezine 196 - Learn PowerShell 2.0 Commands
If one of your New Year resolutions is to learn something new, then
PowerShell v 2.0 is a great choice. Of all the scripting programs
in the world, no
package enables you do more as quickly and with so few instructions, as
PowerShell. My mission is to help you learn PowerShell via a
series of 10 ezines. If you are in a hurry, check out back-numbers that I have
already published.
Topics for Learning PowerShell v 2.0 Commands
♣
The benefits of learning PowerShell maybe more long-term than you
realized. In the old days you may have been a whiz kid with Windows
3.1's file manager, but you could not call yourself a techie unless you
spoke DOS proficiently. Now as we enter the twenty tens or tweens,
even if you have mastered the latest Windows Explorer you cannot claim
to be a top administrator unless you know the basics of PowerShell.
Discovering PowerShell is like cooking, in that even
amateurs can knock up a meal, yet professionals can do so much more
with same ingredients. My point is that you will see a huge range of
abilities at PowerShell, don't be put-off when you see the pros make
dazzling programs with flashy code. You can collect a tremendous
amount of information by just using simple one-line commands.
Please hold the thought that learning PowerShell is not about jumping
on the latest bandwagon, it really will save you time, and it will
extend what you can achieve in the way of configuring a computer.
The start of a new decade is a classic time to master a new skill.
My mission is simple, to get you up and running with PowerShell v 2.0.
In this ezine I will explain how to download the program and then start
issuing simple PowerShell commands.
The Basics - Installing PowerShell v 2.0
How to launch PowerShell depends very much on your operating system.
Select the procedure appropriate for your version of Windows, then find
the shortcut to PowerShell ISE (the GUI version). If you cannot
see a link, try Start Menu, Search and type 'powershell'.
Windows 7 With modern operating systems such as
Windows 7 or Windows Server 2008. PowerShell is built-in to the
operating system; therefore all you need to do is go to the Control
Panel and 'Turn Windows feature on'. (Select PowerShell!)
Vista or XP With operating systems such as Vista,
that were around before PowerShell 2.0 RTM was developed, you need to
visit Microsoft's
Support, and SCROLL DOWN and find Windows Management Framework Core,
then select the appropriate PowerShell / Framework package for your
computer.
Warning for Vista and XP only Guy says: Compared
with other Microsoft software packages, which are easy to get and
install, the procedure for finding PowerShell v 2.0 is confusing.
Paradoxically, once you realise searching for the .msi package is harder
then usual, then getting your copy becomes easy. Trap: Avoid the
CTP versions which often come to the top of any internet search for
PowerShell.
Once you get a local copy, double click the .msi file and follow the
setup instructions, watch out for instructions to install .Net
Framework.
PowerShell v 1.0 In truth, Microsoft has not
rewarded those who installed v 1.0 with an easy upgrade path.
Those early adopters will have to uninstall PowerShell v 1.0 and then
install version 2.0 together with the latest .Net Framework. This
is not a trivial task; seek extra information on the Microsoft Support
website.
WinRM The killer feature of PowerShell v 2.0 is
remoting, which means issuing commands at your computer which are then
run on another network machine. For Vista and older machines this
could
mean an additional download of WinRM. My advice is to get started with the basic
PowerShell 2.0, and only when you have got it running take a timeout and
get WinRM working. I suggest this not because remoting is
unimportant, but because psychologically, building on success makes learning
satisfying, whereas grappling with too many new systems risks failure
and frustration.
For the following commands I am assuming that you have launched
PowerShell, preferably the ISE or GUI version. Our mission is to start
interrogating the operating system using the built-in verb-Noun commands
called cmdlets. To this day, when I am learning a new PowerShell
technique I like to have the GUI version open so that I can
troubleshoot. Once I have mastered the technique I abandon the GUI
and work solely in PowerShell. If it would help you understand
what's we are trying to achieve, try opening the Windows Explorer
alongside PowerShell.
List Files in the System32 Folder
You are most welcome to copy these commands and paste them into PowerShell.
Actually, you would getter better feel for the language if you typed
them.
get-ChildItem -path C:\windows\system32
Note 1: Try adding the filter *.exe after ChildItem: get-ChildItem
-path *.exe C:\windows\system32
Try other locations for -path, for example C:\Program Files
get-ChildItem *.exe -path "C:\Program Files" -recurse
Note 2: Observe that "Program Files" has a
space, this is why we enclose this path in double quotes.
Note 3: By all means try without -recurse. The point is that
this parameter enables us to drill down into
sub-directories. To see more parameters for any cmdlet call for
help thus: get-Help get-ChildItem -full.
Select Properties and Sort
One of the most useful of all PowerShell's command is get-Member; its
purpose is to reveal more properties and methods for another cmdlet, in
this way we can make
better scripts.
get-ChildItem -path "C:\Program Files" | get-Member
Note 4: Observe the | symbol. This tells PowerShell to pipe
the output of the first command (get-ChildItem) into the second part
(get-Member)
Next, we can use the Directory property to sort, and also the
Directory and Name properties in the table output.
get-ChildItem *.exe -path "C:\Program Files" -recurse | `
Sort-Object Directory | format-Table Directory, Name
Note 5: Observe the backtick `. This tells
PowerShell that the same command continues on the next line.
Remember in PowerShell, as with most script languages, there is no
word-wrap, consequently a new line would normally mean a fresh
command.
Challenge 1: Try sorting on different
properties, for instance Name. Also try selecting more properties in the
output, for example, CreationTime
Calculating IP Address
ranges is a black art, which many network managers solve by creating custom
Excel spreadsheets. IPAT cracks this problem of allocating IP addresses
in networks in two ways:
For Mr Organized there is a nifty subnet
calculator, you enter the network address and the subnet mask, then IPAT
works out the usable addresses and their ranges.
For Mr Lazy IPAT
discovers and then displays the IP addresses of existing computers.
Download the Free IP Address Tracker
Investigate more PowerShell cmdlets (verb-Noun pairs)
Discover which other cmdlets contain the verb 'get'.
Challenge 2: I would not want you think that
'get' is the only verb, so please try this: get-command -noun
service
Note 6: PowerShell invariably uses singular
nouns, thus we type: Service (and not ServiceS)
List Services
get-service | Where{$_.status -eq 'running'}
Note 7: A little 'reverse engineering' may help
here. get-Service | get-Member will once again unearth the
properties. Plain get-Service will reveal two values for
status: Stopped and Running. I admit that the Where{clause} is
a bit of a jump from what we have done so far. But now that I
regard you as a member of the scripters' fraternity we all learn by
copying other people's code!
Note 8: Attention to detail, PowerShell is
obsessed with the minus sign, thus it's -eq (not =). One more
point, 'Running' needs to be in single quotes.
Observation: My whole rationale is to get
people started. However, I want to emphasise that PowerShell has
all the pure programming techniques of VBScript and other scripting
languages, it's just that for beginners I choose not to employ them.
I regard PowerShell as a dual purpose language, people can master simple
administrative commands in half an hour, yet it would take a year of
full time study before you got to the bottom of what PowerShell can
achieve.
Putting this knowledge to use - Restart the Spooler Service
Once your introduction stage is over you will want to look for
PowerShell scripts that are going to save you time. For instance,
should a service such as the spooler be giving trouble, you can often
cure the problem by restarting the service rather than rebooting the
machine. If you regularly have PowerShell open, it's quicker to
type the following command then to ferret around with the Services
snap-in.
Note 9: If this command does not work.
Have a brief study of the error message, but probably the best fix
is to close the PowerShell ISE, then re-launch by right-clicking and
select 'Run as Administrator'. For more troubleshooting launch
the computer's 'Services' and see if you can manually restart the
(print) spooler.
Guy Recommends: SolarWinds Engineer's Toolset v10
This 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 each tool teaches me more about how the
underlying system 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 fully functional trial copy of the Engineer's Toolset v10
Summary of Learning PowerShell v 2.0.
Three 'takeaways': Investigate a cmdlets parameters with get-help
verb-noun -full. To list the properties of any cmdlet append |
get-Member. Research more cmdlets with get-Command.
If you are looking for handy network utilities, try some of the free downloads at
Tools4Ever
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• Introduction • Dreams •
3 Key Commands • Cmdlet scripts • Real life tasks
• Remote PowerShell •
-Online •
WinRm and WSMan •
Learn PowerShell
• PowerShell Home •
Microsoft
PowerShell
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.
Download my ebook: 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.
|