The First Month With PowerShell

About a month ago, I decided that it was time.  I had written(i.e. copy and pasted) a couple of PowerShell scripts in the past to accomplish something but that was no longer good enough.  To be completely honest, I don’t even remember now what gave me the itch to learn PowerShell a month ago but here I am.  Sifting through many resources, watching videos, reading blog posts, rediscovering Twitter.  It’s been a very interesting ride so far.  What have I learned in the last month?  I still know nothing…well, next to nothing now.

My journey began with a trip to Google.  “learn powershell”  Low and behold, the second link is the Microsoft Virtual Academy.  Having perused this wondrous resource before, I knew it would have videos and some great content so I started there.  Getting Started with PowerShell 3.0 Jump Start.  I burned though that in about a day and wanted more so I searched for other PowerShell videos on the MVA.  Wow!  There are a ton on there so away I went.  I have to say, if you are behind like I am, that is probably one of the best places to start.  Fantastic content.

Let’s go over some things that I have learned so far.  First of all, the help system in PowerShell is absolutely fantastic.  Get-Help will be your best friend.  Second, the syntax of the cmdlets is very easy to remember.  Verb-Noun.  It couldn’t be more simple and easy to remember.  So for example you want get information about an AD user.  Hmm, sure enough.  Get-ADUser is the name the cmdlet.

Since I had to take two programming courses in college (why I chose C++ is beyond me), some common concepts in programming I already understood to a degree.  Things such as loops, and operators.  Those courses were finally good for something.  Become familiar with how these work as soon as possible.  They are invaluable to understand and be able to use.

One of the coolest things in my opinion about PowerShell happens to be, at least for myself, the hardest concept to really wrap your head around.  Everything is an object.  Well what does that mean?  The easiest way I know how to explain it is with an example.  Let’s pick something really easy like Get-Date.  Let’s just type that in and see what we get.

Get-Date

Well, that gave us what you probably thought.
Thursday, December 03, 2015 10:49:38 PM

I think I did this next part by accident and figured out what it really meant that everything was an object. Let’s assign that to a variable and then display it’s contents.

$date = Get-Date
$date

Ok, so we saved the current date and time into a variable. So what. Here is the cool part. Each object in PowerShell has some functions that you can use with them. The next best thing to Get-Help is something called Get-Member. If you pipe an object into Get-Member, it gives you all kinds of information about what methods can be used on that object as well as that objects properties. At this point in my learning, it’s the properties that interested me. Let’s take that variable that we saved the current date and time to.

$date | Get-Member

The first thing that I want you to really take notice to besides that wall of text that just floated by is the very first line of output. “TypeName: System.DateTime”. The variable that we saved the current date to is actually saving a DateTime object. Ok, so looking through some of the properties I see “DayOfWeek”. So what does that do? That looks interesting. Let’s try it.

$date.DayOfWeek

That returned just what it sounds like. The day of the week that you saved as part of the current date time from Get-Date. Go ahead and test some of the others. Pretty cool right? Oh it gets better.

Lets say that you want to see the services on your PC.

Get-Service

OK, but you really only wanted to see the running services. Notice the headers of each column. Those are properties that you can filter with. This is when you utilize the pipeline to pass the output of one cmdlet as the input of another cmdlet.

Get-Service | Where-Object Status -eq 'Running'

Great, but I still see a status column which is now not needed. I know the status already because I told it to only give me the services that are running. Also, that name column contains some funny things. I just want the “English” name of the services. Remember where I said previously that objects have properties. Well the column headers are some properties. And we just want the “DisplayName”. We could save that to a variable and see that property from the variable but that is two separate steps. The other way(which again, I think I figured this out by accident) is to wrap the command in parentheses and run methods or view properties that way.

(Get-Service | Where-Object Status -eq 'Running').DisplayName

I have to say, I use this like crazy now if I want to see just one property of something(which is usually the).name.

These are just barely scratching the surface of the capabilities of PowerShell. As I said in the last post, if you are a System Administrator and not already using PowerShell, do yourself a favor and get cracking. Start with the MVA jump start video and go to town. You’ll be glad that you did.

As always, any tips, comments, feedback, or questions are welcome. Thanks for dropping by and I’ll see you soon.

Advertisements
The First Month With PowerShell