Tuesday, March 03, 2009

Basic Windows PowerShell Commands

Windows PowerShell is relatively new shell command interpreter in comparison to the venerable CMD, CScript, and Windows Script Host.

Just like any shell script, it isn't really difficult to learn simply by trial and errors as long as we get the few basic commands down so we can drill further to other commands we seek to use.

The most basic command we should know is, of course, help.

Get-Help

As the command name suggests, it is the single command we use to see the details of a particular command we submit as a parameter to the help command. Listing 1 below illustrates the help command execution.

Listing 1: PS C:\Users\antonius> help help NAME Get-Help SYNOPSIS Displays information about Windows PowerShell cmdlets and concepts. SYNTAX Get-Help [[-name] <string>] [-component <string []>] [-functionality <string []>] [-role <string []>] [-category <string []>] [-full] [<commonparameters>] Get-Help [[-name] <string>] [-component <string []>] [-functionality <string []] [-role ><string []>] [-category <string []>] [-detailed] [<commonparameters>] Get-Help [[-name] <string>] [-component <string []>] [-functionality <string []>] [-role <string []>] [-category <string []>] [-examples] [<commonparameters>] Get-Help [[-name] <string>] [-component <string []>] [-functionality <string []>] [-role <string []>] [-category <string []>] [-parameter <string>] [<commonparameters>] DETAILED DESCRIPTION The Get-Help cmdlet displays information about Windows PowerShell cmdlets and concepts. You can also use "Help {<cmdlet nam e> | <topic-name>" or "<cmdlet-name> /?". "Help" displays the help topics one page at a time. The "/?" displays help for cm dlets on a single page. RELATED LINKS Get-Command Get-PSDrive Get-Member REMARKS For more information, type: "get-help Get-Help -detailed". For technical information, type: "get-help Get-Help -full".

It's so helpful that we can simply type help help at the prompt to display the details of the help command itself. As we examine the command output's "Related links" section, we see three other commands related to the help command. All the three new commands we found here: Get-Command, Get-PSDrive, and Get-Member are part of the important basic commands we will discuss more thoroughly a little later.

Get-Aliases

"Shallow Hal wants a gal."

The second basic command that is most useful to expedite our PowerShell command study is Get-Aliases also aliased as gal. First, we're gonna combine gal with the first command we learned earlier: help in Listing 2. Then, we'll try to see if sending the command twice to the shell prompt, one as the command call itself and another as the paramater to the command call gal gal in Listing 3.

Listing 2: PS C:\Users\antonius> help gal NAME Get-Alias SYNOPSIS Gets the aliases for the current session. SYNTAX Get-Alias [[-name] <string []>] [-scope <string>] [-exclude <string []>] [<commonparameters>] DETAILED DESCRIPTION The Get-Alias cmdlet gets the alternate names for cmdlets, functions, and executable files that have been established for the current session. This collection includes built-in aliases, aliases that you have set or imported, and aliases that you have added to your Windows PowerShell profile. If you specify one or more aliases, Get-Alias gets the alias object and displays its properties, including the object that was aliases, such as the full name of a cmdlet. This feature is made available by the Windows PowerShell Alias provider. RELATED LINKS Set-Alias New-Alias Export-Alias Import-Alias REMARKS For more information, type: "get-help Get-Alias -detailed". For technical information, type: "get-help Get-Alias -full".Listing 3: PS C:\Users\antonius> gal gal CommandType Name Definition ----------- ---- ---------- Alias gal Get-Alias

See! It's simple right? From Listing 2, we find the usage details of the command gal, and from Listing 3, we find that gal itself is an alias of the command Get-Aliases.

But, but, but... How did I know in the beginning that help and gal are commands we can execute? The answer is as ordinary as it could be—I guessed the first command: help presuming that a program usually has a variation of help, --help, --h, man or the old-school /? in some sort of form even without reading its documentation. As for the command: gal, after having found out that while the command help could be executed and yields an output indicating the actual command name: Get-Help, I simply sought in MSDN PowerShell site a reference entry pertaining to available command aliases.

Up next... We won't need to go through the details of each command, but will start our PowerShell exercises by using the three new "related" commands we learned earlier from Listing 1.

No comments: