Home > Data Center Tips > > Windows Server 2008: PowerShell basics and top commands
Data Center Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 


Windows Server 2008: PowerShell basics and top commands


Microsoft PowerShell Team
02.29.2008
Rating: --- (out of 5)


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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

Rate this Tip
To rate tips, you must be a member of SearchDataCenter.com.
Register now to start rating these tips. Log in if you are already a member.




BROWSE BY TAG
Information systems management,   Hardware and performance monitoring,   Data center operations management,   Server hardware,   Rack mount server and x86 hardware,   VIEW ALL TAGS

Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   



RELATED CONTENT
Hardware and performance monitoring
Application performance monitoring firm targets cloud computing
Mission impossible: Data center asset management
Check server specs before upgrading your operating system
Reduce chances of hardware failure with preventive server maintenance
Sys admins: Are your config files and scripts hacks or products?
Systems management tools: Microsoft takes aim at the Big Four
HP downsizes data center cooling monitor: News in brief
Zenoss upgrades IT monitoring software to vie with Big Four
Indemnification, support woes plague open source systems management
Capacity planning tools tutorial for Linux and Unix

Rack mount server and x86 hardware
Reporter's notebook on AFCOM Data Center World: Day two
IT services consolidation: Data centers weigh risks
Users buying, configuring servers for virtualization
New eBay data center director dishes
Blade server popularity cools
Vendors pledge joint support for Cisco's Unified Computing System
Users question the benefits of multicore processing
Defining 'unified computing systems'
Apps testing key in upgrade to six-core processors
Roadmap for Sun Microsystems customers after the Oracle acquisition

RELATED GLOSSARY TERMS
Terms from Whatis.com − the technology online dictionary
automated test equipment  (SearchSoftwareQuality.com)
DCML  (SearchDataCenter.com)
event forwarding  (SearchDataCenter.com)
HP OpenView  (SearchDataCenter.com)
lights-out management  (SearchDataCenter.com)
MIS  (SearchDataCenter.com)
smoke testing  (SearchWinDevelopment.com)

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.



Database Programming Solutions - .NET XML, Visual Studio LINQ, ORM .NET
HomeNewsTopicsITKnowledge ExchangeTipsBlogsMultimediaWhite PapersEvents
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides technology professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective purchase decisions and managing their organizations' technology projects - with its network of technology-specific websites, events and online magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Site Map




All Rights Reserved, Copyright 2005 - 2009, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts