Ezine 171 – Reasons for Learning Windows PowerShell

Ezine 171 – Reasons for Learning Windows PowerShellReasons for Learning PowerShell

Let me begin with an emotional reason for learning Windows PowerShell; it is simply the most intuitive and forgiving language for interrogating a Windows operating system.  The logical appeal of learning PowerShell is because its represents the future of command-line scripting.

It’s easy to get started with PowerShell, because when Microsoft created this shell language for Windows, they built-in aliases which run all your old DOS commands such as dir (there are also aliases for UNIX and other shell languages).  Furthermore, you can run native operating system commands such as ipconfig or ping, just as you did in a DOS box. 

Trust me; once you begin your journey into PowerShell, you will see more and more advantages.  If you have to configure Windows Server 2008, or a future Windows version, not only will it be quicker to configure the system with a few PowerShell commands, but also there are some operations that are just not possible via the GUI menus.  Therefore the sooner you abandon that old DOS box and start using Windows PowerShell, the sooner you will feel the muscle of its command-line configuration.

The Split Between PowerShell for Server Administrators, and PowerShell for Developers

Whenever you see a PowerShell article, think, ‘Is this a snippet to help ordinary systems administrators? Or, ‘Is this technique for a PowerShell developer’.  My articles are aimed at the former, people who know DOS, or who have flirted with VBScript, and are now willing to experiment with PowerShell.  I want to make you comfortable configuring the operating system with a 3 or 4 word PowerShell cmdlet.  I would also like to show you how to create simple 5 to 10 line cmdlets with loops and script blocks.  To achieve my aim of getting you started with Windows PowerShell I will be publishing examples in this fortnightly ezine.  After that, I will pass you over to articles aimed at the programmer / developer.

When I say PowerShell is easy to get started I mean it; but it’s also my duty to tell you that it’s as difficult as any scripting language to be an expert.  PowerShell has gears, you can soon learn enough to interrogate your operating system.  It’s not difficult to make simple configuration changes, but it’s going to take time to reach top gear and produce complex scripts with error correcting code.  I want to emphasise that there is a dichotomy between what the Windows Server Administrator needs for everyday use, and what a top developer can create with PowerShell.

The beauty of PowerShell is that anybody can learn the basics in two hours.  Thereafter, what happens is people add-on modules for specialist areas, for example PowerShell for Exchange 2007, PowerShell for services and process, or even PowerShell for WMI.

I would like to give you an example of PowerShell’s ‘modular learning’.  Once you have a few lines of code that display on screen you may want to save the results, for instance, eventlog errors, the list of current process, or the inventory of services that are running.  No problem, just append | out-File YourPath.txt.  The first time I tried to save VBScript output to disk, it took half an hour of fiddling with the code.  In PowerShell it takes only ten seconds to save results to a file, moreover, you have learned a valuable module with a myriad of applications.

The Catharsis of Abandoning VBScript

If you are already experienced with VBScript, then it will be a cathartic moment when you abandon those skills.  I know because it happened to me when I made the switch from VBScript to PowerShell.  However, like me, you will soon cheer up when you see how much easier it is to script in PowerShell than it ever was VBScript.  Manipulating file objects in general, and outputting scripts to a file is trivial in PowerShell, whereas it was a pain in VBScript.

Is there anything that VBScript does better than PowerShell?  Yes, Active Directory scripting.  However, with QAD snap-ins PowerShell at least catches up with VBScript, and may even overtake it for easy of use.

Exchange Monitor from SolarWindsGuy Recommends: The SolarWinds Exchange Monitor

Here is a free tool to monitor your Exchange Server.  Download and install the utility, then inspect your mail queues, monitor the Exchange server’s memory, confirm there is enough disk space and check the CPU utilization.

This is the real deal – there is no catch.  SolarWinds provides this fully-functioning freebie, as part of their commitment to supporting the network management community.

Free Download of SolarWinds Exchange Monitor

Exchange 2007 – A Case Study for Adopting Windows PowerShell

Please take the time to investigate the role of PowerShell in Exchange Server 2007, principally because this will be the model for future Microsoft operating systems.  For instance, Exchange 2007 does not support any WMI classes; this is why you must use PowerShell commands to configure that email server. 

I would like to offer another insight into Exchange 2007 and PowerShell; while you can Manage Exchange Server 2007 via the Exchange Management Console, all the GUI commands are translated into PowerShell before they are executed.  Perhaps you can now see why many administrators prefer to type a few PowerShell commands rather than grapple with 3 levels of menus. 

Exchange Server 2007 shows how easy it is to get started using PowerShell for real work, for instance:

get-MailboxFolderStatistics

get-Mailbox -Identity "Guy Thomas"

enable-MailUser -Identity Guy -ExternalEmailAddress guy@ cp.com

The first ‘ah-ha’ movement
English is my first language, yet it was only when I studied a foreign language that I realized that there things called verbs and nouns.  PowerShell re-enforces this point because its building blocks are all just verb-Noun pairs.  For those of us weak at spelling, and prone to syntax errors, another bonus is that all the nouns are singular.

Following the main PowerShell verb-Noun cmdlet are one or more switches.  You will recognize them because they are introduced with a minus sign, moreover, they are called -parameters.

One more thing; there are some commands that don’t exist in the Exchange 2007 GUI, thus for certain tasks you simply must use PowerShell.  The good news is that these are just a verb-Noun cmdlet plus one parameter; for example:
new-AutodiscoverVirtualDirectory -websitename yourdom.com

Talking of Exchange, Guy Recommends: The SolarWinds Exchange MonitorExchange Monitor from SolarWinds

Here is a free tool to monitor your Exchange Server.  Download and install the utility, then inspect your mail queues, monitor the Exchange server’s memory, confirm there is enough disk space and check the CPU utilization. This is the real deal – there is no catch.  SolarWinds provides this fully-functioning product for free, as part of their commitment to supporting the network management community.

Free Download of SolarWinds Exchange Monitor

PowerShell Command to Get you Started with Any Operating System

What I particularly like about PowerShell is the way you can do real scripting ‘stuff’ right from the word go.  While it is straightforward to get started, each new building block that I learn enables me to create ever more complex and rewarding scripts.  With other script languages that I have flirted with, I either had to start with babyish ‘Hello World’ examples, or else I had to copy and paste stuff without understanding what the code did.  Let me give you three short and ridiculously easy examples of PowerShell doing real ‘stuff’.

  1. get-EventLog -list
  2. get-Service | where {$_.status -eq ‘Running’}
  3. get-WmiObject -list | where {$_.name -match ‘Network’}

My main danger here is making you believe that PowerShell is ‘too easy’.  Let me assure you that can graduate to normal scripting processes such as functions and loops.  My point is that PowerShell is so clever that ordinary mortals can do real and exciting scripting; you can learn by doing, you don’t have to begin with a grounding in deep programming theory.

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.

Guy Recommends: Tools4ever’s UMRAUMRA The User Management Resource Administrator

Tired of writing scripts? The User Management Resource Administrator solution by Tools4ever offers an alternative to time-consuming manual processes.

It features 100% auto provisioning, Helpdesk Delegation, Connectors to more than 130 systems/applications, Workflow Management, Self Service and many other benefits. Click on the link for more information onUMRA.

Summary of Reasons to Learn Windows PowerShell

I would like to make a rational appeal for you to embrace PowerShell as a replacement for DOS.  In addition, consider whether it will eventually render VBScript obsolete.  If that seems a bit strong, mull over whether PowerShell will supersede VBScript as the language of choice for controlling the operating system.

Other rational reasons for learning PowerShell include configuring Exchange Server 2007, making it easier to interrogate WMI objects.  Also saving the output of your script to a text file is a trivial task in PowerShell compared with VBScript equivalent.

I would also like to make an emotional appeal; PowerShell is the most satisfying language or skill that I have learned.  But then, I have always wanted to be a script writer. 

If you like this page then please share it with your friends

 


See more Windows PowerShell tutorials

PShell Home   • Introduction   • Dreams   • 3 Key Commands   • PowerShell Help About   • Get-Help

PowerShell v 3.0   • Set-ExecutionPolicy   • Get-Command   • Cmdlet scripts   • Import-Module

PowerShell Version Check   • Backtick   • PowerShell examples   • PowerShell ISE   • Get-Member

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.