Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  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 


Reader Comments
none

MitchellJames March 17, 2008 (Article Rating: )


Thanks

tambarogod June 29, 2008 (Article Rating: )


good start!

pink_mantra August 13, 2008 (Article Rating: )


useful

sashok001 September 18, 2008 (Article Rating: )


useful book

sashok001 September 18, 2008 (Article Rating: )


Building an Effective Reporting System

sashok001 September 18, 2008 (Article Rating: )


You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
Microsoft: Save Money ... By Paying for Software

Microsoft this week adopted an interesting tactic in its long-running battle with open source software: Businesses looking to save money over the long haul should simply pay for software instead of moving to free, open source solutions. The rationale? ...

Command Prompt Tricks

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

PsExec

This freeware utility lets you execute processes on a remote system and redirect output to the local system. ...


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

Windows OSs Whitepapers Why SaaS is the Right Solution for Log Management

Related Events SQL Server 2008 – Can You Wait? | Philadelphia

PowerShell 101 - eLearning Series

SQL Server 2008 – Can You Wait? | Atlanta

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 Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


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 Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2008 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing