PowerShell for Windows Admins

Nov 29 2009   11:16AM GMT

PowerShell – what is it?

Richard Siddaway Richard Siddaway Profile: Richard Siddaway

I am going to assume at least a passing acquaintance with PowerShell as we start this series. I will attempt to explain as we go. If there is something in the scripts that you don’t understand please leave a comment and I will post an explanation.

PowerShell is the automation engine for the Windows environment. It was introduced (as version 1) in November 2006 as a free download. Exchange 2007 was the first major product to incorporate PowerShell support. This has spread to SQL Server 2008 and Windows Server 2008 (optional feature but not on Sever Core) among others and now PowerShell is part of Microsoft’s Common Engineering Criteria so all major products will be expected to support it.

In October 2009 PowerShell v2 was released. This is also available in Windows Server 2008 R2 and Windows 7 where it is installed and turned on by default. In Windows Server 2008 R2 Server Core PowerShell is available as an optional feature. In this series I will assume PowerShell v2 but a lot of the scripts will run on v1.  Wherever possible I will give an alternate coding for v1.

PowerShell is normally thought of as the shell and the scripting language but it can be embedded and used with programs. The GUI administration console in Exchange 2007/2010 is built on top of PowerShell cmdlets.

PowerShell is based on .NET.  The cmdlets use .NET objects and the pipeline between the cmdlets passes .NET objects.

IMPORTANT POINT – You do NOT have to be a .NET programmer to use it.  A lot of the examples we will see can be run from the command line.

PowerShell commands are known as cmdlets. They consist of a verb-noun pair separated by a hyphen e.g. Get-WmiObject. This makes the cmdlets more or less self-describing.

The verb should be from the approved verb set (type get-verb at prompt) and the noun should describe the object being worked with – in this case a WMI object. We will see a lot of this cmdlet as we progress through WMI.

WMI can be used in VBScript and you can find many examples on the Microsoft, and other, web sites. PowerShell brings an ease of use to WMI that just changes the game completely. The major gain is the ease of display of the data. Let’s see some examples.

PS> Get-WmiObject -Namespace ‘root\cimv2’ -Class Win32_Share

Name                Path                                Description
----                ----                                -----------
ADMIN$              C:\Windows                          Remote Admin
C$                  C:\                                 Default share
D$                  D:\                                 Default share
HP DeskJet 812C     HP DeskJet 812C,LocalsplOnly        HP DeskJet 812C
IPC$                                                    Remote IPC
print$              C:\Windows\system32\spool\drivers   Printer Drivers
Users               C:\Users

WMI is arranged in a series of hierarchical namespaces. Each name space contains one or more classes. The root\cimv2 namespace is the PowerShell default so we could use

PS> Get-WmiObject  -Class Win32_Share

And get exactly the same results.

WMI gives us a way to access information on remote machines, without PowerShell being installed, so we could use

PS> Get-WmiObject  -Class Win32_Share -ComputerName rslaptop01

Better still we can use the PowerShell pipeline to help us get information from a number of machines.

PS> “.”,”rslaptop01″,”″ | foreach {“`nComputer: $_”; Get-WmiObject -Class Win32_Share     -ComputerName $_ }

This takes three computer names (or IP addresses) pipes them into a foreach (alias for Foreach-Object  – – we’ll cover aliases another time). For each name we print out the computer name after throwing a line using `n. $_ is used to represent the object coming down the pipeline. PowerShell allows us to substitute into strings to build our display. The ; is an end of line  marker.

We then use Get-WmiObject to retrieve the shares with $_ representing the computer names.

Think of the situation where you have taken over administration of a number of file servers. One thing you will need to know is what shares are defined on those machines. This is one way to get that information quickly and easily.

Next time we will look at what else we can discover about our shares.

 Comment on this Post

There was an error processing your information. Please try again later.
Thanks. We'll let you know when a new response is added.
Send me notifications when other members comment.

Forgot Password

No problem! Submit your e-mail address below. We'll send you an e-mail containing your password.

Your password has been sent to:

Share this item with your network: