Introduction to PowerShell
To start at the very beginning, we answer the most basic question, "What is PowerShell?" PowerShell is a command-based shell and scripting language built on the .NET framework with incredible power and versatility designed especially for system administration. The latest version as of this writing is version 2.0 and it was officially released on October 28, 2009. We say shell and scripting language because both components can be used separately. At its fundamental level, it is a command-based shell in a console-like interface that will allow you to simply issue it commands and call its powerful scripting language to accomplish what tasks you need it to do. In this interactive use, you directly issue commands to the console and it performs the desired tasks. The other powerful part of PowerShell that takes full advantage of the underlying scripting language is the ability to run longer or complex instructions from a script. This allows you to automate some of the tasks you might need to perform. The final piece which is brand new to version 2.0 is the ability to run commands on one or more computers from a single computer running PowerShell. These abilities together should allow you to tackle almost any job without problem.
Another aspect of PowerShell you need to become familiar with is its command syntax. The commands PowerShell uses are called cmdlets. It is through these simple instructions that you will be performing all the common system administration tasks. cmdlets all share a similar syntax composed of a verb and a noun in a verb-noun format. As an example, we will use one of the most helpful commands, get-help. The get-help cmdlet is an easy example. Typing Get-help followed by the subject or command you want to get help about will get you more information about it. It will either give you the help you need or give you a list of subjects so that you can narrow it down to which one you need (Table-1).
You will get a chance to try some cmdlets of your own later on in this tutorial, but for now, it is enough that you simply see what they look like if you have not before.
As you can see, the main goal of Windows PowerShell is to give the administrator more control over the system and make it easier to perform the required tasks. It increases the administrator's bag of tools by adding automation and remoting and their associated advantages. Imagine only having to write a command once and then being able to run it multiple times on different machines. This eliminates the need to run the same thing over and over by simply having PowerShell let you run it multiple times. It also eliminates some of the human error aspects since you only need to write it once and therefore only check it once. In this tutorial, we will cover many of the new commands for Windows Server 2008 R2 and try to give you a feel for them and what they are supposed to do.
Table-1 Getting Help Is Easycmdlet Description Get-Help about Will list all the subjects that start with about Get-Help Will get the specific help file about_scripts about_scripts Get-Help Will give you a list of all the subjects that *about* have "about" anywhere in them
What is new in PowerShell V2
Version 2.0 of Windows PowerShell was officially released on October 28, 2009 after much anticipation by the PowerShell community. It introduces a large amount of new features that were not available before. It stays true to its purpose of making system administration easier by adding remoting, debugging tools and other things to the administrator's new shiny toolkit.
Of all the new features in Windows PowerShell version 2.0, the one feature that seems to elicit cheers and get the most attention is the new remote management or remoting feature. PowerShell supports interactive remoting where you connect directly and issue commands, fan-in (1 pc to many) and fan out (many pcs to 1). It allows the administrator to run scripts on one or many remote computers with a single command. Gone are the days of tediously going to different computers and running the same script on each of them one at a time. Gone are the days of taking your preferred script and copying it from machine to machine. Now, all you have to do is establish a session to the other computer or computers you want to run the script on and then issue the commands. From running a single cmdlet to a whole script, it is that simple. PowerShell 2.0 does have to be installed on the computers you are running the scripts on, the originating one and the target ones.
Another new feature sorely lacking from the earlier versions of PowerShell is the ability to run background jobs. This new ability of PowerShell version 2.0 to run background jobs allows you to asynchronously run other jobs or processes in the background while you continue to work on your session. You issue the command for the background job and immediately get the prompt back so that you can continue to work while the background job executes. These background jobs can be run on your local machine or on a remote computer.
Windows PowerShell 2.0 has also included new script debugging capabilities. Included now is the built-in capability to debug your scripts. It allows you to set breakpoints, step through code, monitor variable values, and more. It also allows you to decide what actions to take when the break point is hit. These capabilities give you the tools necessary to debug all your scripts with relative ease.
Another new feature is that of eventing. Eventing is the new capability of PowerShell to handle and react to events. It now includes an infrastructure that not only can listen for system and application events and act based on them. In addition, you can now create your own events which allow you to set up a chain of actions based on what your scripts do.
The new version also comes with a large number of new cmdlets. PowerShell version 2.0 introduces a little over 100 new cmdlets with numerous capabilities. All these cmdlets range from all the new functionalities mentioned earlier to powerful logging tools and mail-sending cmdlets.
PowerShell 2.0 also supports script internalization. This allows PowerShell to display messages in multiple languages. In addition to the regular get-help command and man command at the command line, PowerShell includes online help via a parameter to the get-help command. Using get-help, the command to get help on and then the online parameter opens a browser for you with the command help in Microsoft TechNet.
Another new feature is the support of Modules. A module is a package that includes Windows PowerShell commands like cmdlets, providers, aliases, and variables. PowerShell now lets you create these modules and organize your commands so that you can share them with others. PowerShell version 2.0 has also included a lot of advanced functions. These functions behave just like cmdlets but they are fully written in the Windows PowerShell scripting language as opposed to C#.
Another new feature that PowerShell 2.0 has brought is the new Integrated Scripting Environment or ISE. The PowerShell ISE lets you run commands and scripts in an integrated development environment (IDE). It allows you to edit and debug your scripts with many options to figure out what is wrong and where it is going wrong like most graphical interfaces. It makes writing cmdlets easier with multiline editing and line and column numbers. It includes color coding like most graphical interfaces which makes it very easy for the user to write and execute their own commands by immediately showing them if they have made any syntax errors. It has several panes or windows to make it easier to see what is going on. One pane to write your command(s), one to show your scripts, and one to show you the output or results of your commands or scripts. In essence, it makes it really easy to get started with PowerShell by giving the user the tools to create what they want quickly and easily.