Powershell Power! (Part Deux)
Okay, so this is the second installment of my little tutorial-thingy for Powershell. After much thought on where I want this to go, I figured the next logical step would be to talk about setting up your Powershell (“PoSh”) environment.
So, let’s put together a little laundry-list of tools you may want to procure. Just download the bits and save them for later. We will go through some of these in more detail.
- Powershell installation bits (requires at least .NET 2.0 Framework)
- PowerTab from ThePowerShellGuy (Most EXCELLENT Site for all things PoSh)
- Quest PowerGUI (A powerful FREE GUI and IDE for PoSh) I won’t cover this installation here, but it is pretty straight forward.
That will probably be enough to get moving.
The Powershell install should be fairly straight forward. Just take all the default options and let-her-role.
NOTE: If you want to install a newer version of Powershell, you will need to Uninstall the previous version. This is kinda sucky, but not too bad. To Uninstall it, you can go to Add/Remove Programs and look for it, but you may not see it if you have “Show Updates” option off. It probably will show up as “Window Powershell”.
To install PowerTab, unzip the downloaded file to a folder you want it to live permanently. I normally pick something like “C:\Program Files\PowerTab” so it is where all my other programs live.
Now open a command shell and start Powershell by typing in “powershell” and hitting Enter.
This may take a few seconds to launch, but if successful you should see your new and shiny Powershell prompt. The first thing to do before anything else make it so we can actually run scripts.
Out-of-the-box, Powershell is delivered secure. SO secure, in fact, that you can NOT run ANY scripts without making it a little less secure. I will probably discuss this in more detail later or pressure my buddy Mike to write about it on his blog (this is more up his alley than mine).
Anyway…at your PoSh prompt type in the cmdlet:
Set-ExecutionPolicy RemoteSigned
If successful, you should be kicked back to your prompt. Now we can move forward with the PowerTab install.
CD to the directory you install PowerTab to and run the “Setup.CMD” file to start the installer. I normally just take all the defaults and let it go. This will probably take a few minutes to finish up.
Now to show you a little about what PowerTab does for you….
If you type “Get-” and then hit the TAB key you should see a popup window with all sorts of fun things. Basically PowerTab is like tab completion on STEROIDS. It is very helpful for discovering what is available and such. I hope you will take this opportunity to explore Powershell a bit.
You can get a list of all commands (called Commandlets) by typing in “Get-Command”.
I guess that is a fairly good place to break and talk about the “Cmdlets” (said CommandLets).
Cmdlets are the meat-and-potatoes of Powershell. They are to Powershell what “cd” and “dir” are to the normal Windows command shell (cmd.exe). Cmdlet names are constructed of 2 parts: a VERB and a NOUN. If you look at the output of the “Get-Command” cmdlet, you will see lots of verbs on the left side of the “dash” in the cmdlet name: “Get”, “Out”, “Write”, etc. The right side of the cmdlet name is the noun, or the thing that the verb acts on.
As you can see there are common Verbs and Nouns. You can, in fact, do a “get-command -verb get” to list all commands that have the verb “Get”. You can do the same for Nouns. Go ahead and try that out on a few.
There are several cmdlets you should be using as you are learning the ins and outs of Powershell. Probably the MOST helpful is, well “Get-Help”.
Get-Help is one I use on a VERY regular basis. The general format for this one is:
- “Get-Help <Cmdlet>” : Gives basic info on the cmdlet
- “Get-Help <Cmdlet> -Detailed” : Gives MUCH more info about the Cmdlet including some examples on using the cmdlet
- “Get-Help <Cmdlet> -Full” : I think this gives ALL the info about the Cmdlet
- “Get-Help <Cmdlet> -Examples” : Just gives you the usage examples.
Go ahead and try it now. Do a “get-command”, pick one that looks interesting to you and “get-help” on it. This was crucial for me in learning the Powershell.
While I bring this part to a close I will challenge you to write something…well ANYTHING really. The best way for me in learning was to pick a task and do it in Powershell. The thought process for me was “Well, I could do this in vbScript in like 5 minutes, or I could take 30 minutes and do it in Powershell and learn a ton.” If I have the time to do it in Powershell, I am doing it.
Some simple tasks would be to start using Powershell as your “normal” command shell. This will get you used to using the cmdlets to navigate the environment and doing things. Try NOT to use some of the default aliases that are “replacements” for the standard “cmd.exe” commands. These would include “type”, “dir”, etc. I have been using the Powershell equivilant aliases… “dir” becomes “Get-ChildItem” or simply “gci”.
The other thing I will challenge you to look at is the cmdlet “Get-Member”. This will help you in this worthwhile venture into the wonderful world of Powershell.
I think the next session we will cover the notion that Powershell is “Object-Based”, which will be a good lead in from your “homework” on the “Get-Member” cmdlet.
Anyway, until next time…. happy Scripting!
– Mark