• subscribe
January 30, 2008 12:00 AM

PowerShell 101, Lesson 1

How to Get- what you need to create basic PowerShell commands
Windows IT Pro
InstantDoc ID #97742

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.



Related Content:

ARTICLE TOOLS

Comments
  • ashok
    4 years ago
    Sep 18, 2008

    Building an Effective Reporting System

  • ashok
    4 years ago
    Sep 18, 2008

    useful book

  • ashok
    4 years ago
    Sep 18, 2008

    useful

  • Sean
    4 years ago
    Aug 13, 2008

    good start!

  • John
    4 years ago
    Jun 29, 2008

    Thanks

You must log on before posting a comment.

Are you a new visitor? Register Here
  • SP1?
    I know there is a SP1 for SQL 2008 R2 available....and there is a "feature pack" as well... ...
  • SQL database mirroring
    I have SQL Server 2008 R2 Enterprise 64bit on Windows 2008 R2 Enterprise 64bit.  Each SQL Server has...
  • Dell Compellent Disk Drive
    Does anybody has experience with Dell Compellent Disk Drive? Basically, this system manages all disk...
  • Sql server performance tuning
    I need to find a tool that help me to optimize sql server,queries,improve the performance and solve ...