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

The Position category tells you whether a parameter must be named or whether it can be referenced by its position. When Position is set to named, you must include the parameter’s name when referencing that parameter. When Position is set to a number, you can reference the parameter by its name or you can simply provide the parameter’s value in its correct position.

For example, as you can see in Figure 3, the -path parameter is required for Get-Content. However, you can include that parameter value in the first position without including the parameter name, as in

Get-Content c:\sample.txt

If a parameter value contains spaces, you must enclose the value in quotes.

In the PARAMETERS section, each parameter name is followed by information in angle brackets (< >). This information specifies the type of data that the parameter value must be. As Figure 3 shows, the -path parameter value must be a string. If a set of brackets ([ ]) follow the word string, then a string array is permitted as the parameter value.

In the case of switch parameters, which don’t take values, the data type will read . For example, Get-Content’s -force parameter is defined with this data type. This parameter overrides restrictions that might prevent the command from succeeding. The override occurs only when you include the parameter in your command.

One other feature to note about parameters is that PowerShell includes a parameter-name completion feature. You need to include only enough of the parameter name to distinguish it from other parameters. For example, the command

Get-Content c:\sample.txt -force

is the same as

Get-Content c:\sample.txt -fo

Besides providing the parameter information that you need to build commands, the Help file for Get-Content includes examples of how to use the cmdlet, helpful tips in the Notes section, and resources where you find additional information. The best part is that Help files are available for all the cmdlets—there are even Help files that discuss general concepts.

Getting Help with Concepts
PowerShell includes a set of Help files that provide overviews of various concepts. Each file begins with “about_” and ends with the name of the topic. To view an alphabetical list of the about topics, run the command

Get-Help about*

To view information about a specific topic, you simply include the topic’s full name as a parameter value. For example, to retrieve the file about flow control, run the command

Get-Help about_flow_control

Figure 4, shows part of the results you can expect. As you can see, the file provides an overview of how to implement flow control in a Power- Shell script.

Using Aliases
Some of the cmdlet names can be quite verbose, an annoying characteristic if you have to continuously retype commands. Fortunately, PowerShell supports the use of aliases for referencing cmdlets. An alias is an alternate name that’s usually much shorter than the actual cmdlet name. PowerShell includes a number of built-in aliases, and you can create your own aliases.

To view the aliases available to your current session, run the Get-Alias cmdlet. Current session refers to your current connection to PowerShell. When you start PowerShell, you start a new session; that session persists until you close PowerShell, which ends your connection. In addition to displaying all built-in aliases and their associated cmdlets, Get-Alias displays any aliases you created in the current session and aliases defined in profiles, which are user-defined configuration settings loaded into PowerShell whenever it starts. (Profiles will be discussed in a later lesson.)

If you want to view the aliases available for a specific cmdlet, you must qualify the Get- Alias cmdlet. For example, to view the aliases available to the Get-ChildItem cmdlet, run the command

Get-Alias | 
Where-Object {$_.definition `
-match “Get-ChildItem”}

This command incorporates several elements that I’ll explain in detail in subsequent lessons. For now, all you need to know is that the results of the Get-Alias cmdlet are sent to a Where-Object cmdlet that filters out any results that don’t match Get-ChildItem. If you want to check for aliases for a different cmdlet, replace Get-ChildItem with the cmdlet name.

As you can see in Figure 5, PowerShell includes three aliases that reference Get- ChildItem: gci, ls, and dir. You can use any of these aliases in place of the cmdlet name. All four of the following commands list the contents of the C:\Windows folder:

Get-ChildItem c:\windows 
dir c:\windows
ls c:\windows
gci c:\windows

To create an alias within the current session, use the Set-Alias cmdlet. For instance, to create an alias named cnt that references Get-Content, run the command

Set-Alias cnt Get-Content

You can then use cnt wherever you would use Get-Content. The alias is available until you end your session (i.e., close PowerShell). Note that you can’t include parameters when defining an alias, only the cmdlet name itself. If you want to define a reference to a cmdlet and its parameters, you should create a function. You’ll learn how to create a function in a later lesson.

Moving Forward
In this lesson, I introduced you to the fundamental components necessary to begin exploring and using PowerShell commands, which consist of one or more cmdlets. In upcoming lessons, you’ll learn more about how to use these cmdlets and how to create scripts that enable you to leverage Power- Shell’s full capabilities. In the meantime, begin working with cmdlets. Use Power- Shell’s Help file to create commands and learn about specific concepts. Try out the different parameters and learn how to create and use aliases. In no time at all, you’ll be ready to incorporate PowerShell into your daily routines.

End of Article

   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 be a registered user or online subscriber to comment on this article. Please log on before posting a comment. Are you a new visitor? Register now




Top Viewed ArticlesView all articles
WinInfo Short Takes: Week of November 9, 2009

An often irreverent look at some of the week's other news, including some more Windows 7 sales momentum, some Sophos stupidity, Microsoft's cloud computing self-loathing, more whining from the browser makers, Zoho's "Fake Office," and much, much more ...

Command Prompt Tricks

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

Windows 7 Sets Sales Record

Microsoft CEO Steve Ballmer described Windows 7's first ten days of sales as "fantastic" while in Japan yesterday. ...


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 WinConnections and Microsoft® Exchange Connections

Deep Dive into Windows Server 2008 R2 presented by John Savill

PowerShell 201 - eLearning Series with Paul Robichaux

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