Summary: In today’s guest article, Microsoft PowerShell MVP Shane Hoey reminds us to focus on the object.
Microsoft Scripting Guy Ed Wilson here. Today we have a guest article by Windows PowerShell MVP Shane Hoey. Here is what Shane has to say about himself: “My scripting experience started back when Windows XP was the new kid on the block, and soon found myself scripting on a daily basis with VBScript, WMI, and ADSI . Fast forward to the Scripting Games 2009, and I finally discovered Windows PowerShell, and I’ve been hooked ever since. One of the things I like most about Windows PowerShell is its ability to easily automate, especially my daily repetitive tasks. My background is in system and network administration, but at work they just call me the PowerShell Geek these days! By the way, I also run the PowerShell Usergroup Brisbane.
Take it away, Shane.
I’ve changed my background to HTML color #012456, I need some inspiration, and this time http://drscripto.tv is just not what I need! After all, I’ve been asked to write a Hey, Scripting Guy! Blog post.
My buddy Chris Brown who recently started a Windows PowerShell user group in Melbourne, Australia, was on Twitter as usual. In fact, he’d give Sean Kearney a run for his money when it comes to being passionate about Windows PowerShell. And so the Twitter conversation started:
@shanehoey: Hey @chrisbrownie, any ideas I can steal/borrow for a Windows PowerShell blog article I need to write ? I have scripter’s block
@kittenstix: @shanehoey how to clean up Active Directory accounts using PowerShell & scheduled tasks
@ChrisBrownie: @shanehoey umm.. how about how awesome PowerShell is and why SysAdmins should learn it?
@jamesbannan @shanehoey how to manage Hyper-V and VMware VMs using PowerShell 😉
@Brendonrd RT @Kittenstix @shanehoey how to clean up AD accounts using powershell & scheduled tasks - PS>format domain$ /q /y =)
I sometimes think Twitter was created for the Windows PowerShell community!
A few more tweets arrived, and I started to think about how I started scripting back in 2009. I remember thinking, “OK, this can’t be too hard. It looks just like the cmd box I’m used to. Hey, I can kind of even understand VBScript, so this is going to be a breeze.” And I started off by typing in Help.
I wrote my first Windows PowerShell script that night. It was my first submission for the 2009 Scripting Games Event 1. Well, I guess you could call it a script, but little did I realize the horror of Event 3 would stick with me for a long time to come. I still remember it to this day! In hindsight, it really was not that hard. It’s just that I did not understand the basic PowerShell principles.
Here is the exciting thing. Those cmdlets I learned to use during the 2009 Scripting Games—Get-Help, Get-Command, and Get-Member—I still use on a daily basis.
So the Twitter conversation continued.
@shanehoey: @jamesbannan have you seen this ? http://psdu.co/pvJABy
@shanehoey: @Brendonrd have you seen http://psdu.co/qhobXk Free PowerShell Training in November
SHAMELESS PLUG ALERT Yes, that’s right. Shane and Chris are running Free PowerShell Training in November. It’s based on the More Lunches series by Don Jones, so grab the book and join us for some free training in November.
So it seems like previous Scripting Guys articles have already covered all the blog suggestions from Twitter.
I’m staring at some Lego on my desk wondering, what can I blog about? Then it hit me: “Always focus on the object.” When I’m helping IT pros to start learning Windows PowerShell, I often talk about “Always focusing on the object.” I say it all the time, but what do I really mean by it ? Well, let’s have a look:
Get-process | sort PM -descend | Select -first 10
Let’s break down to each command by running Get-Member, and take note of the object’s type, property, and methods.
Get-process | get-member
Get-process | sort-object –property PM -descending | Get-Member
Get-process | sort-object –property PM -descending | Select -first 10 | Get-Member
So the object has not changed, but remember that it can indeed change. This time, we are going to run a similar command again:
Get-Service | Select-Object –property Name | Format-list Name
So once more I want you to use Get-Member and discover a bit more about the objects in the pipeline:
Get-Service | Get-Member
Get-Service | Select-Object –property Name | Get-Member
Get-Service | Select-Object –property Name | Format-List Name | Get-Member
Hey. that’s cool. By using the Get-Member cmdlet, we can quickly see how important it is to “focus on the object” as it traverses the pipeline. Let’s look at this a bit closer:
Get-Service | Select-Object –property Name | Get-Member
Get-Service | Select-Object –first 10 | Get-Member
Did you see that? Want a hint? Have a closer look at the object’s type, and you’ll notice that it’s been changed.
So hopefully through these simple examples, you can see the importance on “focusing on the object,” especially as it passes thru the pipeline. Unfortunately, we don’t have time today to dig too deep. Oh, and by the way, one more thing: don’t forget to watch out for pipeline input ByValue and ByPropertyName!
I want to thank Shane for his article today. I especially want to thank Shane and Chris for their efforts in spreading the word about Windows PowerShell. Check out their free training series in November. It is sure to be awesome. Join me tomorrow as I begin a new week on the Hey Scripting Guy! Blog.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy