Friday, March 06, 2009

Essential Windows PowerShell Commandlets

Previously, we learned the basic commands: help and gal. I suppose now it's time to properly refer to these "commands" as commandlets or cmdlets following PowerShell's own set of terminologies. But just to note that whenever both commands and commandlets, I use them interchangeably for the same semantic meaning. Call me stubborn. Call it bad habit. But they really are all just commands to me. In any case, although I think of all of the commandlets simply as commands, we should still mentally note at the back of our mind that for "data-typing" purposes, PowerShell truly types these items or commands as Cmdlet.

Our first essential command after learning the basics is Get-Command

Get-Command

Get-Command outputs all of the available commands in PowerShell in a clean PowerShell installation. These are the commands that we really will use as the building blocks for writing PowerShell scripts. Through a process of trial and errors, let us try to see if we can perform the obligatory, introductory of any programming lesson: print the string "Hello World!" on screen (I promise I'll keep the trial and errors to an abridged version).

  1. Type-in Get-Command at the prompt
  2. Observe the output and see if any of the command contains descriptions that indicates it is a command for printing anything on-screen by using guess-words such as: "Print", "echo", "Write", "Response", or any other words from whatever your other experience may have been.
  3. If nothing on-screen indicates as such, then you'll want to type in help [Cmdlet name] at the prompt to read the details one-by-one. Hey at least that's how I started learning PowerShell, by reading documentation as well as by trial and errors.

But, let's use my experience, shall we? Let's just type in the following simple command, and we'll simply get the result we want just as we'd see in Listing 1.

Listing 1: PS C:\Users\antonius> gcm | where {$_.definition -match ".*print*"} CommandType Name Definition ----------- ---- ---------- Cmdlet Out-Printer Out-Printer [[-Name] <string>] [-InputObject <psobje... ps c:\Users\antonius> gcm | where {$_.definition -match ".*echo*"} CommandType Name Definition ----------- ---- ---------- Cmdlet Set-AuthenticodeSignature Set-AuthenticodeSignature [-FilePath] <string []> [-C... PS C:\Users\antonius> gcm | where {$_.definition -match ".*write*"} CommandType Name Definition ----------- ---- ---------- Cmdlet Write-Debug Write-Debug [-Message] <string> [-Verbose] [-Debug] ... Cmdlet Write-Error Write-Error [-Message] <string> [-Category <errorcat... cmdlet write-host write-host [[-Object] ><object>] [-NoNewline] [-Separ... Cmdlet Write-Output Write-Output [-InputObject] <psobject []> [-Verbose] ... Cmdlet Write-Progress Write-Progress [-Activity] <string> [-Status] <strin... cmdlet write-verbose write-verbose [-Message] ><string> [-Verbose] [-Debug... Cmdlet Write-Warning Write-Warning [-Message] <string> [-Verbose] [-Debug...

From these output we can sort of guess which command we can use, but if in case we're still in doubt we could always do help write-debug, help write-error, etc. Soon after we go through this we'll find the command we want to use is Write-Output.

That's a rather "long" command to type, though. Let's try to see if there's a shorter version of it. For this purpose, let's try what's on Listing 2.

Listing 2: PS C:\Users\antonius> gal | where {$_.definition -match "Write-Output"} CommandType Name Definition ----------- ---- ---------- Alias write Write-Output Alias echo Write-Output

Now that we found what we want, we can try either command aliases just as in Listing 3

Listing 3 PS C:\Users\antonius> write "Hello World!" Hello World! PS C:\Users\antonius> echo "Hello World!" Hello World!

Sure enough, we found both yield the same result

We might wonder, "Why are there two aliases for the same command?" I don't really know the answer to that question other than perhaps the alias write is the built-in alias for Write-Output and perhaps the alias echo is simply a way to help those who are used to writing .BAT scripts to transition a little more easily to Windows PowerShell. Whatever the answer to that question is rather irrelevant; what's more important is that now we've gone through the basic exercises in learning each PowerShell command.

Wait wait wait!!! We're not done here!

What's all that business with the where command and the rest of the strings following it?

No, I didn't cheat :)

I simply wanted to shortcut all the trial and errors I went through in learning PowerShell without filling the content of this notes full of garbage. Trial and errors, after all, are things we each can do individually on our own time. As for the shortcut strung with the where command, I'll note an explanation of it another time.

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.