Working with PowerShell v2
- What Is PowerShell?
- What's New with PowerShell v2?
- Great PowerShell Resources Online
PowerShell is both a command-line tool and an automated scripting platform. Much of the press has reported misconceptions about the purpose of PowerShell. It isn't a replacement for CMD.exe (which is evident in the fact that Windows 7 includes both). It is an automation-scripting platform with multiple APIs that is accessible to the PowerShell CLI (command-line interface).
PowerShell v1 was downloadable for Vista and XP. PowerShell v2 is also downloadable for Windows 7, but it's also included with the operating system. What does that mean to you?
Well, depending on the type of user you are, it may mean absolutely nothing. If you never even use the command prompt, then an additional command prompt isn't important to you. So this article simply explains what PowerShell v2 is doing on your Windows 7 computer.
If you are an avid command-prompt user for various DOS command-type uses, you might find this an interesting discussion but it's not a reason to switch over. For example, let's say you use the command prompt to locate your IP address by typing ipconfig at the command prompt. Can you do this with PowerShell? Absolutely! But do you need to use PowerShell to do what you have always done in the command prompt? Not at all. In fact, some would say it will take you longer for those simple commands because it takes PowerShell a little longer to come up (whereas Cmd.exe pops up almost instantaneously).
What Is PowerShell?
Basically, it is an extensible command-line shell. You can locate it under Start, All Programs, Accessories, Windows PowerShell. At first glance, you see little more than a command prompt with a blue background (Figure 1).
PowerShell integrates with and requires the .NET Framework and the types of commands you execute are called cmdlets (command-lets), which are actually part of the .NET classes.
Cmdlets are somewhat reasonable in the look of them because they hold a verb-noun structure. For example, a very simple cmdlet for obtaining assistance is Get-Help. Some of the more popular verbs are Get, Set, Remove, Test, Enable, Disable, Install, Uninstall, New and Move. So you can see how the syntax for running a single cmdlet is not all that complicated.
So here is an easy example of what you might use the Get-Help cmdlet for. Let's say you have a cmdlet called Get-Process but you don't know how it works. You can type the following in PowerShell:
Get-Help Get-Process
This shows you all the information you need about a cmdlet. Figure 2 shows that the results include a Synopsis, the Syntax, a Description, Related Links, and Remarks.
Although the cmdlets themselves are self-descriptive, it still takes some time getting used to them and how they work. Especially when you start to delve a bit deeper into what they can do when combined with other cmdlets through a feature called pipelining.
You see, cmdlets produce some form of result that can be displayed onscreen or (because the results are objects) can be used in harmony with other cmdlets in a pipeline using the | character.
So here is a really cool PowerShell command that uses a pipeline structure to first get all the services running on your computer, then sort those items based upon if they are running or stopped, and then show them to you in a formatted table. We are looking at using three cmdlets: Get-Service, Sort-Object, and Format-Table. It would take on the following form in the pipeline, with the result shown in Figure 3:
Get-Service | Sort-Object Status | Format-Table
Now PowerShell can accomplish in one line what used to take tons of code prior to PowerShell. And PowerShell can go even further for those who wish to truly delve deeply into the depths of PowerShell and really explore the scripting aspects of PowerShell, although typically developers and administrators are the ones who will pursue that level of information.
A great book for PowerShell to help take you to the next level is Windows PowerShell Unleashed, published by Sams. It is especially worthy because it covers all of the new v2 features, including PowerShell 2.0 remoting!