By submitting my Email address I confirm that I have read and accepted the Terms of Use and Declaration of Consent.

By submitting your personal information, you agree that TechTarget and its partners may contact you regarding relevant content, products and special offers.

You also agree that your personal information may be transferred and processed in the United States, and that you have read and agree to the Terms of Use and the Privacy Policy.

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:

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-".

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:

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.

1 comment

Register

Login

Forgot your password?

Your password has been sent to:

By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy