Windows IT Pro is the leading independent community for IT professionals deploying Microsoft Windows server and client applications and technologies.
  
  
  Advanced Search 


February 2008

PowerShell 101, Lesson 1

How to Get- what you need to create basic PowerShell commands
RSS
Subscribe to Windows IT Pro | See More Systems Administration Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!
SideBar    PowerShell Empowerment

Executive Summary:
Windows PowerShell provides far more power and flexibility than traditional Windows command shells, such as cmd.exe. To help you take advantage of that power, this six-article series explains how to use PowerShell to perform various tasks. In this article, you'll learn how to start using PowerShell and how to run basic PowerShell commands. You'll also learn how to get help within PowerShell when creating those commands and how to use aliases in your commands.


By now, most administrators are familiar with Windows PowerShell. Many have downloaded it, played with it, and perhaps used it to perform ad hoc tasks as they might do in Windows command shell (cmd.exe). But PowerShell is a lot more than a simple DOS-like command shell. It’s a command-line and scripting environment that leverages the Microsoft .NET Common Language Runtime (CLR) and .NET Framework. When you work in the PowerShell environment, you’re working with .NET objects. The folder structures you view or the services you access are actually instances of objects that represent the folders and services, unlike other command shells that simply process text. As a result, PowerShell provides far more power and flexibility than traditional command shells.

To help you take advantage of this power, I’m writing a series of articles that explain how to use PowerShell to perform various tasks. Each article is a lesson that builds on previous lessons in order to demonstrate important PowerShell concepts. In this lesson, I explain how to get started using PowerShell and how to run basic PowerShell commands. I also show you how to get help within PowerShell when creating those commands and how to use aliases in your commands.

Getting Started
PowerShell currently doesn’t ship with Windows OSs, although this will change starting with Windows Server 2008. You can find Power- Shell download links and information on the Windows PowerShell Web page (www.microsoft.com/powershell). Before you download PowerShell, install .NET Framework 2.0 if you don’t already have it. The PowerShell installation process is quick and straightforward. Just be sure to install the PowerShell edition specific to your OS. Microsoft provides editions for Server 2008 beta 3, Windows Vista, Windows XP SP2, and Windows Server 2003. For this article, I’m running PowerShell on XP.

After PowerShell is installed, you can use it immediately. To run PowerShell, select All Programs under the Start menu, choose Windows PowerShell 1.0, and click Windows Power- Shell. When the PowerShell window appears, the command prompt displays the current working folder (C, on my system). You’re now ready to start writing and executing PowerShell commands.

Working with Cmdlets
PowerShell supports its own scripting language, which is based on the .NET Framework. The most basic command in that language is the cmdlet (pronounced command-let). A cmdlet is similar to a function in that it performs a specific task, such as retrieving a folder’s contents or updating a registry entry.

PowerShell includes more than 100 built-in cmdlets. You can create additional cmdlets, but you must create them in a .NET language, such as Visual Basic .NET or C#. (The Power- Shell 101 series will discuss only the built-in cmdlets.) Each cmdlet is in the form verb-noun because Microsoft wanted to use a consistent naming scheme to make PowerShell easy to learn and expand. The verb specifies the action to be taken. The noun indicates the type of object involved. For example, the Get-ChildItem cmdlet retrieves a list of items in the current working directory or container, such as the registry. To run the cmdlet, type it at the PowerShell command prompt and press Enter. The results are displayed beneath the command prompt. That’s all there is to running a basic command.

There will probably be times when you don’t know whether there’s a cmdlet for the task you need to accomplish or when you can’t remember a cmdlet’s name. You can view a list of all cmdlets by using the Get-Command cmdlet. Figure 1 shows part of this list, which includes the cmdlets’ names and syntax, but not a description of what the cmdlet does. To get that information, you can use the Get-Help cmdlet.

Getting Help with Cmdlets
PowerShell includes a set of Help files that you can access directly from the Power- Shell command window with the Get-Help cmdlet. To retrieve Help information about a specific cmdlet, you use Get-Help with its -name parameter followed by the name of the cmdlet you want to learn about. Like parameters in cmd.exe commands, parameters in PowerShell cmdlets provide information that the cmdlets need to do their job. Unlike parameters in cmd.exe commands (which might start with a hyphen, a slash, or no symbol at all), parameters in PowerShell cmdlets always begin with a hyphen, which is another example of PowerShell’s consistent naming scheme.

Now let’s take a look at an example to demonstrate how this works. A common system administrator’s task is to read text files. After looking at the list of cmdlets that Get-Command provided, you think the Get-Content cmdlet might do the trick but you aren’t sure. To retrieve Help information about Get-Content, run the command

Get-Help -name Get-Content

As Figure 2 shows, this command returns a description of the cmdlet and syntax information. The command returns the content of an item, which in this case refers to any type of file in a system. In the past, you might have used the For command for batch files or the File- SystemObject object in a Windows Script Host (WSH) script, but in PowerShell, you simply use the Get-Content cmdlet. You can retrieve more detailed information about the syntax by adding the -full parameter to the command

Get-Help -name Get-Content -full

Notice that the -full parameter doesn’t take a corresponding value. This type of parameter is called a switch parameter because it switches the behavior of the cmdlet.

Figure 3 shows some of the information returned by this command. (On your computer, you’ll need to scroll or resize your window as necessary to view the entire contents.) The PARAMETERS section provides the information you need to include parameters in your command. Two important categories of information for each parameter are Required and Position.

The Required category tells you whether the parameter is mandatory or optional. When Required is set to true, you must include the parameter. When Required is set to false, the parameter is optional.

   Previous  [1]  2  Next 


Top Viewed ArticlesView all articles
Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...

2009 Windows IT Pro Editors' Best and Community Choice Awards

Picking a favorite product from an impressive crowd of competitive offerings is never an easy task, and such was the case with our Editors' Best and Community Choice awards this year. ...

WinInfo Short Takes: Week of November 23, 2009

An often irreverent look at some of the week's other news, including some post-PDC some soul searching, a Google Chrome OS announcement and a Microsoft response, Windows 7 off to a supposedly strong start, the Jonas Brothers and Xbox 360, and so much more ...


Related Articles PowerShell 101, Lesson 2

PowerShell Script Lets You Check Patches' Status

How to Get Information About Installed Applications Without Using WMI

PowerShell Pointers

Scripting Whitepapers From Development to Production: Streamlining SharePoint Deployment with DocAve Deployment Manager

Related Events Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

PowerShell 101 - eLearning Series

Check out our list of Free Email Newsletters!

Scripting eBooks Keeping Your Business Safe from Attack: Encryption and Certificate Services

Best Practices for Managing Linux and UNIX Servers

Building an Effective Reporting System

Related Scripting Resources Introducing Left-Brain.com, the online IT bookstore
Looking for books, CDs, toolkits, eBooks? Prime your mind at Left-Brain.com

Discover Windows IT Pro eLearning Series!
Clear & detailed technical information and helpful how-to's, all in our trademark no-nonsense format


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro DevProConnections IT Job Hound
Left-Brain.com Technology Resource Directory asp.netPRO ITTV Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 © 2009 Penton Media, Inc. Terms of Use | Privacy Statement