I posted about getting more in depth with PowerShell a couple weeks ago. I am using the excellent book by Don Jones and Jeffrey Hicks “Learning PowerShell 3.0 In A Month Of Lunches

Day 4 – Commands: Formatting, Parameters and lots of typing!

Day 4 starts off with learning the basic formatting of PowerShell commands. PowerShell consists primarily of “commands” that come in the form of a VERB-NOUN format followed by parameters. Sometimes the parameters a explicitly required, sometimes they are optional. I discovered a couple of ways to get help with figuring out PowerShell commands

Common Verbs - Get, Test, Start, Remove, New, Add

Nouns have much more variety and will continue to become more diverse as more applications and components adopt PowerShell.

I discovered a means of pulling back a list of all commands containing a particular verb or noun. The following command will retrieve a list of all commands using the “Get” verb

Get-Command -Verb Get

image

You can obviously substitute other verbs in place of “Get” to retrieve the commands associated with those verbs.

Likewise, this command will retrieve a list of all commands containing the noun “Service”

Get-Command -Noun Service

image

Of course you can also substitute other nouns just as we can with verbs.  Also, you can use wildcards like the * to find even more variety.

----------------------------------------------

Aliases let us define a shortcut of sorts to save typing or create commands we can execute with fewer keystrokes, or even just make it easier to remember. There are a number of pre-defined aliases already built into PowerShell that can be listed with the GET-ALIAS command.

image

You can create your own aliases with the NEW-ALIAS command. For instance, if I wanted to create an alias STOPSVC to be used for the STOP-SERVICE command I would issue the following -

New-Alias stopsvc Stop-Service

Interestingly, there is already an alias in PowerShell that does the same thing called SPSV. But we are allowed to have different, unique aliases for the same commands so both work and do the exact same thing. One thing to note is that for any PowerShell scripts you intend to release to the public or even just to co-workers, unless they have defined the same custom aliases, their systems will not be able to interpret the alias and the script will fail. Also, aliases do not persist between PowerShell sessions so to keep your aliases intact requires us to export and import the aliases which, of course, could be scripted with PowerShell but lends to additional work. I think I will likely avoid using aliases in general practice.

----------------------------------------------

Show-Command is one of my favorite PowerShell help utilities. particularly with commands that have a lot of parameters that can be specified. A great example of this is the GET-DISK command. It has a large number of parameters that can be passed to get specific disk information as we can see in the help for this command –

image

I know my limitations and one of those is the ability (actually, lack of…) to remember large numbers of similar patterned text items. The great thing about SHOW-COMMAND is that it will produce a mini-GUI with all of the parameters allowing us to fill them in and then produce the PowerShell command formatting for us. Issuing the following command will give us that mini-GUI

Show-Command Get-Disk

image

Now we just fill in the values we want, click COPY to copy to clip board, then paste to the PowerShell command line. Or we can just RUN it as is.

VERY cool!

Chapter 5 review is next!

 

-Cheers!