Tip

Windows Server 2008: PowerShell basics and top commands

The Microsoft PowerShell team offers up the top commands and usage scenarios for the new command line shell and scripting language for Windows Server 2008

This week SearchDataCenter.com tapped the engineers at the Microsoft PowerShell team to outline the basics of Windows PowerShell and list the top commands and usage scenarios.

What is Windows PowerShell?
Microsoft Windows PowerShell is a command-line shell and scripting language with more than 130 standard command-line tools and consistent syntax and utilities. The new feature runs on Windows XP, Windows Vista and Windows Server 2003. Windows PowerShell is now included as part of Windows Server 2008 and can be evaluated in Windows Server 2008 Beta 3, Exchange Server 2007, System Center Operations Manager 2007, System Center Data Protection Manager V2, and System Center Virtual Machine Manager.

For the first time, Windows PowerShell is included as part of the Windows operating system and is one of a slew of new manageability features in Windows Server 2008 including the new Server Manager, Server Core, Event Viewer, Task Scheduler, and Reliability and Performance Monitor.

Top PowerShell usage scenarios
So what can PowerShell do for you? The MS PowerShell team put these usage scenarios together:

Managing command-line services, processes, registry and WMI data
Common as-needed server administration tasks, such as identifying running services or processes, viewing the registry and reading and changing settings stored in Windows Management Instrumentation (WMI), are easier with the built-in command-line tools (cmdlets) get-service, get-process, get-wmiobject, and the registry provider for Windows PowerShell.

Terminal Server management
Because Terminal Server stores a wealth of data in WMI, administrators can automate Terminal Server configuration changes by means of Windows PowerShell scripts, and examine configuration similarities and differences across a Terminal Server farm. See the numerous script examples in the TechNet ScriptCenter.

Deploying and configuring Internet Information Services 7.0 (IIS 7.0)
Windows PowerShell is able to manage IIS 7.0, including deploying and configuring IIS 7.0 across a Web farm. Learn more at IIS.net and Channel 9.

Top PowerShell Commands:
Here are some of the top PowerShell commands, tips and tricks from the PowerShell team:

The top command for working with WMI is Get-WMIObject. For example, to get information about your BIOS, you can just do:

PS (415) > Get-WmiObject win32_bios | select serialnumber

serialnumber
------------
MXK5380BK3 NA580

To get a list of all of the installed services:

PS (423) > (get-service).count
131

And to find out what services are running…

PS (424) > (get-service | where {$_.status -eq "running"}).count
77

There are lots of examples of using PowerShell to manage Exchange Server at the Exchange Server Wiki. As an example, the following script will display a table showing mailbox size statistics:

get-mailbox | get-mailboxstatistics | select-object

At this point, you may notice a pattern. Commands to get information in PowerShell start with "Get-".

Moving on to Virtual Machine Manager (VMM), you can download the VMM cmdlet reference document here.

Here's an example from that document:

EXAMPLE 1: Copy a physical hard disk from a source machine to a virtual hard disk file.

PS C:\> $Credential = Get-Credential
PS C:\> Get-VMMServer -ComputerName "VMMServer1.Contoso.com"
PS C:\> $VMHost = Get-VMHost -ComputerName "VMHost01.Contoso.com"
PS C:\> Copy-HardDisk -SourceComputerName "P2VSource.Contoso.com" –VolumeDe
viceID "C" -Credential $Credential -VMHost $VMHost -Path "C:\MyVHDs" –Fixed
-DiskSizeAdd 1024

The first command uses Get-Credential to prompt you to supply a user name and password and stores your credentials in variable $Credential. The required credentials for this operation are either a local Administrator account or a domain account with administrator rights on the computer on which resides the physical hard disk that you want to convert to a virtual hard disk.

The second command connects to VMMServer1 in the Contoso.com domain and retrieves the server object from the Virtual Machine Manager database. The following commands use this server by default.

The third command gets the object that represents the host named VMHost01 in the Contoso.com domain and stores the host object in variable $VMHost.

The last command copies and converts the "C" volume located on the source computer named P2VSource (in the Contoso.com domain) into a new virtual hard disk (still named "C") and places it on VMHost01 at the specified path (C:\MyVHDs). The Fixed parameter specifies that the .vhd is fixed rather than dynamic in format and the DiskSizeAdd parameter increases the size of the volume by 1024 MB. As this command is processed, $Credential provides your credentials to Copy-HardDisk.

Here are some quick examples for IIS7.

To start and stop the Web server service, user the Start-Service and Stop-Service cmdlets.

Start-Service -servicename w3svc

Stop-Service -servicename w3svc

If you have the IIS WMI provider installed, then you can use the Get-WmiObject cmdlet to list the websites on the server.

Get-WmiObject -namespace "root\WebAdministration" -class Site

Obviously the Get-WmiObject cmdlet allows you to do a lot of things if you know which classes to work with. The way to do this is to use the –List parameter on Get-WmiObject. But this can produce a lot of information. For example, on my system:

PS (426) > (get-wmiobject -list).Count
930

There are 930 WMI classes available, so you need to be able to search for specific classes. In PowerShell you can do this with the Where-Object command, just like we did in the get service command example above, only this time we want to search on the name property. This command will find all of the WMI classes with "computersystem" in their name.

get-wmiobject -list | where {$_.name -match "computersystem" }

From this query, we find the Win32_ComputerSystem class. The next thing we might want to know is what properties this class has. We can use the Get-Member cmdlet to do this.

get-wmiobject Win32_ComputerSystem | get-member –type property

This lets us see that there is a property. Let's display this property in megabytes. We can do this as shown:

PS (26) > (get-wmiobject win32_computersystem).totalphysicalmemory / 1mb
2046.4765625

Another important source of information for administering a system is the event log. PowerShell lets you use the same approach to getting information from the event load as was used with other information sources. Here's an example that shows how to scan the 100 newest events in the event log for any errors.

get-eventlog system -newest 100 | where {$_.entrytype -eq "error" }

Other PowerShell tips
PowerShell is a shell, but it can also be loaded into other programs as an embedded scripting language, like Windows Scripting host (but using .NET instead of COM.) There are a number of examples where other people have built new "hosts" for PowerShell, such as PowerGUI and PowerShell Plus. It is also possible to use this technique to build a GUI overtop of the PowerShell engine so you can support both graphical and command line experiences.

Even with all of the fancy stuff, one thing to keep in mind is that PowerShell is still a shell or command-line interface just like cmd.exe. Sometimes people forget this and ask things like "how can I start notepad from PowerShell," thinking that you'll have to do something complex as you would in VBScript. But, because PowerShell is a shell, all you have to do to start notepad is to type "notepad."

Another shell-related questions we see is, "With cmd.exe, I use the attrib command. How would I do that in PowerShell?" The answer is use "attrib" just like in cmd.exe.

For more info, check out:
The Windows PowerShell Toolbox
The PowerShell script center
Windows PowerShell 2.0 CTP

Dig Deeper on IT systems management and monitoring