4 min read | 1137 words | 277 views | 0 comments
PowerShell isn't by any means new. Designed by Jeffrey Snover and initially released in 2006, PowerShell is now on its 6th stable release and has infiltrated workplaces everywhere. While it hasn't entirely displaced the Command Prompt, which itself came about in 1999 to emulate MS-DOS, it certainly allows administrators to spend less time at the black shell and more time at the blue one. While there are many cases where the regular Command Prompt is sufficient (mainly because the PowerShell prompt takes noticably longer to load), network admins everywhere are using PowerShell, so here's a gentle nudge to see what PowerShell can unlock for you if you haven't delved deeper into it yet.
A great place to start is the MVA series "Getting Started with Microsoft PowerShell". While this series from 2013 focuses on PowerShell 3.0, it features two funny hosts, including Jeffrey Snover himself, walking viewers right through the basics of PowerShell, and it's arguably one of the top MVA courses of all time. For even more PowerShell awesomeness, you can check out "Advanced Tools & Scripting with PowerShell 3.0 Jump Start" and "Getting Started with PowerShell Desired State Configuration (DSC)".
Many diehard Unix loyalists refuse to believe that PowerShell could supposedly be better than the bash shell they know and love. What scripters need to understand is that PowerShell is fundamentally different from bash — it deals with objects rather than text. This is where the real power of PowerShell comes from: the ability to easily manipulate, sort, analyze, etc. This isn't to say Unix is terrible: PowerShell actually leverages the best of UNIX and addresses its shortcomings. But I'll let the creator of PowerShell do the rest of the talking:
Yeah, so a lot of people take a look at PowerShell and they say, "Oh, it looks very much like UNIX" — and by the way, just so you know, we actually tried to use UNIX. We didn't start off wanting to invent something. We started off saying, "I got to solve a customer problem". So there were some really great tools out there, the UNIX tools. So my first effort was to get those tools available on Windows, but it turned out that didn't work and the reason why is because UNIX is very much a document oriented operating system, which is to say that a lot of the management is done through document or files whereas Windows is an API oriented operating system. Now, both have their advantages, but here's the thing: the management is completely different. So when I got BASH, and AWK, and SED and GREP available on Windows it didn't help that much, why? Then you go and say AWK, AWK doesn't work against the registry, GREP doesn't work against WMI, SED doesn't work against Active Directory. So in UNIX, those tools, those text manipulation tools, are management tools, but on Windows they are just text manipulation tools and there is not much text so it didn't help. So we had to invent our own stuff, and we wanted to invent as little as possible and so we leveraged a lot of the concepts of UNIX, the compositional model, but UNIX (love UNIX, you guys did a great job) but on the other hand it has weaknesses as well. — Jeffrey Snover, Don't fear the shell (25:28)
Now, the trick is, and here's the magic, is that we spew these objects out and we can then work on them. And when they finally get to the display, the display says, oh I've got an object, what am I going to do with that? There's no way I'm going to burp out this binary stuff and people are going to figure it out. What the display does, it says now I'll convert it to an object or convert it to text. And the whole point about that is you want to delay the conversion of text as far as possible, as long as possible. Because once you have text, it's really hard to operate with. Now, you Unix guys, anybody with a Unix background, you know what I'm talking about, because imagine you wanted to get all those things where the handles were greater than let's say 700. Let's do this. Get-Process. So you see there's a bunch of these things, and some of them are high. So let's say you wanted to get all those that were greater than a thousand. How would you do it. In Unix, if this is text, it gets text, how due do that? Let's see you've got to cut off, one, two, three lines and you go and say I'm going to cut off zero to... let's see, that's one, two, three, four, five, six, seven, seven... I've got to cut off these first seven characters and what do you get? And the answer is a string. So how do you do a string comparison that's greater than a thousand? Well, a thousand is actually easy. You could truncate all the spaces and then if the length is greater than three then it's greater than a thousand. But if you wanted to do it greater than 900, then you've gotta sum up... so you get where I'm going. You Unix guys, you know, hats off to you; I don't know how the heck you've been coping all these years. We call this prayer-based parsing (like please work). It's sort of worked fine, but in our world you don't have to do that. In our world you have these objects. Now, the thing is, when I do Get-Process, I'm displaying an object. Each row is the object itself. And each one of these displays here, each one of these columns reflects the properties. So what did I tell you? I told you we're trying to create a world where you think about what you want, you type it, and you get it. And the way that works is because of objects. So what did I say? I said I wanted to get the processes. I wanted to get those where the handle count was greater than 900. So when I say where the handles are greater than or equal greater than 900 It worked. Why did it work? Because of objects. Objects are so cool. Now, wait. So this is still a little bit messy here, because these handles, I mean look at them — they're all kind of like all over the place. Right? So really what I want to do, I want to sort them. Now, again you cut off three lines and you do this and again it's strings. But no, no, it's an object! So what you can do is again think about what you want, type it, and you get it. You say I want to sort it by handles. Ta da. And then you get it. — Jeffrey Snover, Objects for the Admin (2:28)