Click an Ad

If you find this blog helpful, please support me by clicking an ad!

Monday, April 30, 2012

Powershell and Me

Powershell and Me


My first post is going to be about Powershell, which I have grown to absolutely adore over the past few months.

Background
I've been a computer network administrator for 4 years now, and most of my previous experience with scripting languages and command-line interfaces has been confined to VBScript and the DOS/Windows command line. While I am quite comfortable with writing batch files, I have done my very best to avoid using VBScript except in absolute necessity. For that matter, my cohort knows VBScript well enough, so I can lean on him if I need anything scripted. Throughout the implementation of Windows Server 2008 and the R2 follow-up, as well as Exchange 2010, Lync 2010, and a small Sharepoint 2010 deployment, I have been able to get by with looking up needed Powershell commands online and pasting them in with slight modifications to accomplish what I needed to.

The Trigger
Microsoft released the Windows Server 8 Developer's Preview several months ago, and it became apparent to me that Powershell was going to be more and more important in my day to day management of Windows Servers moving forward. I suppose I already knew that this was true before the Server 8 release, but using the developer's preview made something internally tell me, "Self, you had better get to learning this Powershell stuff." And so I did. I will spare you a "How to Get Started with Powershell" blog post, because brighter minds have already done the topic far better justice than I could. I will, however, lay out a list.

Top Ten Powershell Command for Noobs -- This list is in no particular order!
  1. Get-Help -- Use this command to research Powershell related topics and to get help with how to use commands. I use it quite a bit to find out what the syntax is for a command.
  2. Get-ChildItem -- This is the equivalent of 'dir' in DOS. It returns the contents of a directory. This command was useful as a benign command I could play with without damaging anything while exploring other concepts like piping and switches.
  3. Get-Command -- This command allows you to find other commands. For example, 'get-command get*' will return a list of all of the Powershell commands that begin with 'get'.
  4. Get-Member -- Piping the results of one command into Get-Member will let you see what's possible with the object that was returned. For example, 'get-childitem | get-member' will return a list of properties (things you can see or sort by) and methods (things you can do) for a directory listing object.
  5. Get-Alias -- This shows you all of the aliases. For example, Powershell treats the command 'dir' as if it was running 'Get-ChildItem'. Aliases save massive amounts of typing.
  6. Get-History -- This brings up a list of the last 10 commands you entered. Each command has its own ID number. Another handy shortcut is that if I wanted to run the command with ID number 2 again, I could type 'r 2' and it would run the command again.
  7. Get-PSDrive -- shows you a list of all of your Powershell drives. Sure, all of your filesystems show up, but you might be surprised to learn that Powershell loads many other things as accessible 'drives'. Most notably for me was that I could browse the HKCU and HKLM registry hives as a hierarchy!
  8. Where-Object -- Use this command after a pipe to set criteria on what you want to see, such as filenames with a certain extension.
  9. Sort-Object -- Use this command after a pipe to sort object based on criteria you choose. Note that some commands have a sort switch to handle this built in.
  10. Select-Object -- Use this command after a pipe to specify which properties of an object you wish to see. ProTip: Use get-member to see what properties are available!
I also made the incorrect assumption that things like this would only come in handy at work. Last week I was trying to get a list of all of the MP3 files on my home computer and found that the easiest way to do it was with a Powershell command:

get-childitem -filter *.mp3 -recurse | select name > mp3s.txt

I would like to plug an EXTREMELY useful book I've been using to learn Powershell called Windows Powershell in Action by Bruce Payette. You can find the book at Bruce Payette's website.

In my next post I'll talk about some extremely useful Snap-Ins and Modules that will make Powershell administration really come alive!

No comments:

Post a Comment