Chapter 10. What’s PowerShell?

Although not a replacement for the command prompt (cmd.exe), PowerShell provides an opportunity to really engage the operating system at an under-the-GUI-hood level. This chapter helps PowerShell newbies become comfortable with the concepts of what PowerShell can do and what it was designed for, along with stepping through the syntax of PowerShell commands (called cmdlets) and pipelining commands together to create powerful one-line code.

PowerShell for Beginners

PowerShell is both a new command-line tool and an automated scripting platform. PowerShell v1 was downloadable for Windows Vista and XP, and the same is true of PowerShell v2. The exception in Windows 7 is that PowerShell v2 is included with the operating system. What does that mean to you?

Note

Much of the press has reported misconceptions about the purpose of PowerShell. It isn’t simply a replacement for cmd.exe, which is obvious by the fact that Windows 7 continues to include the Command Prompt. It is an automation scripting platform with multiple application programming interfaces (APIs) that is accessible to the PowerShell CLI.

Well, depending on the type of user you are, it may mean absolutely nothing. If you never even use the command prompt, then having an additional command prompt may not matter, and so this will simply explain what PowerShell is doing on your Windows 7 computer.

If you are an avid command prompt user leveraging various DOS-type commands, you may 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 in 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 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.

Therefore, when we say “PowerShell for beginners,” we mean “PowerShell for power users of the OS who are beginners to PowerShell.” Clear as mud?

What Is PowerShell?

Basically, PowerShell is an extensible command-line shell. You can locate it under All Programs, Accessories, Windows PowerShell and then choose Windows PowerShell. Upon first glance, you see little more than a blue background command prompt (see Figure 10.1).

Figure 10.1. PowerShell with the IPCONFIG command already executed.

image

PowerShell integrates with and requires the .NET Framework, and the types of commands you execute are called cmdlets (commandlets), where the functionality of the cmdlet calls are part of the Framework of the .NET classes.

Cmdlets look somewhat reasonable, in that they hold a verb-noun structure. For example, a very simple cmdlet for obtaining assistance is Get-Help.

Note

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 suppose 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 will show you all the information you need about a cmdlet. Note in Figure 10.2 that the results include a Synopsis, the Syntax, a Description, Related Links, and Remarks.

Figure 10.2. Using Get-Help to get help about the Get-Process cmdlet.

image

Although the cmdlets themselves may be self-descriptive, it will still take 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 together 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 pipe character (|).

So, here is a really cool PowerShell command that uses a pipeline structure to first get all the services that are currently available on your computer and then sort those items based on whether they are running or stopped. It then shows 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 this form in the pipeline, with the result shown in Figure 10.3:

Get-Service | Sort-Object Status | Format-Table


Figure 10.3. Using pipelining to get more power out of PowerShell cmdlets.

image

Here is another great example for IT administrators of how you can utilize PowerShell to perform a long task through the GUI: Let’s say you want to create a new Group Policy Object (GPO), which is something you might do if you are an IT administrator works with Active Directory. Performing this task using the GUI would take you six to eight clicks of your mouse and a line of typing. Performing the same task through PowerShell takes only one to two clicks and two lines of typing. Here is the PS version:

  1. Open a PowerShell session.

  2. Type Import-Module grouppolicy.

  3. Type New-GPO “name of GPO”.

PowerShell can accomplish in one line what used to take tons of code before PowerShell. PowerShell can go even further for those who want to truly delve deeply into the depths and really explore its scripting aspects, although typically developers and administrators are the ones that will pursue that level of information.

A great PowerShell book to help take you to the next level is Windows PowerShell Unleashed, published by Sams. It is especially noteworthy because it was released post-PowerShell v2, and therefore has all the new v2 features, including PowerShell 2.0 remoting!

What’s New in PowerShell v2?

Most of these features are admin-oriented and will not apply to users, but we would be remiss if we didn’t mention some of the great new features in PowerShell v2 that exist on your Windows 7 desktops:

New and improved cmdlets: Some of the nearly 240 cmdlets relate to the following subjects: remoting, converting types, Event Viewer and Event Tracing for Windows (ETW) logs, script internalization, modules, transactions, debugging, eventing, background jobs, WMI, computer, and more.

PowerShell remoting: Using WS-Management, you can now use cmdlets and scripting on remote systems (or multiple systems).

Transactions: Allows transacted operations, which will be exciting only to those who understand that this includes rollback ability. Currently, the PowerShell Registry provider supports transactions (and by extension, rollbacks).

Modules: Script developers and admins can create modules that contain scripts and can be executed in self-contained restricted runspace.

Integrated Scripting Environment (ISE): This is actually an option you can choose under Accessories, PowerShell. It includes a GUI PowerShell host with a tabbed UI for up to eight Unicode-enabled consoles.

Note

There are tons of other features in v2, such as Background Jobs, ScriptCmdlets, SteppablePipelines, Data Language, Script Debugging, Eventing, Network File Transfer, New Operators, Exception Handling with Try-Catch-Finally, Nestable Here-Strings, Block Comments, and last but not least, New APIs.

Some Great PowerShell Resources

Here is a list of some of the top resources promoted at the 2009 Microsoft Management Summit for “What’s New in Windows PowerShell 2.0”:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset