Join Don Jones for an in-depth discussion in this video Run Commands and Shorten the Syntax, part of PowerShell DrillBits.
- One of the difficulties with Windows PowerShell is that it seems everyone has a different way of accomplishing the exact same task. In a lot of cases though, they're really just using a slightly different syntax. For example, let's say I run a command like, "get-Service," and I want to retrieve all of the services whose names start with S. Straightforward enough. Now I want to only keep the ones whose status is running. So I can use the "where-Object" commandlet, give it its filterscript parameter, and then inside the filterscript, I would say, "$_.Status equals Running." The result, should be a list of services that start with S that have a status of running.
When you look at this complete syntax for a command line it's relatively easy to sort of read through it and figure out what it does. Unfortunately, what you'll typically see from folks is abbreviated parameter names. So PowerShell allows you to abbreviate, technically truncate, any parameter name. All you have to do is type enough of the parameter name that PowerShell can differentiate it from other parameters. Let's hop over to another window here and look at the help for "where-Object." You can see that it really doesn't have many parameters "-filterscript," "-inputobject" and all of the common parameters.
So it's possible I could get way with instead of "-filterscript," just typing "-F" or "-FI." It turns out though that this is also a positional parameter and you can tell because the parameter name is in square brackets, although the entire parameter, including its value, is not in square brackets. So I'm going to guess this is a mandatory parameter but I don't even have to type the parameter name. The way to verify that is to look at the full help.
So we'll just run this again with "-full." Looking at the filterscript parameter, I can see that it is required, so it is mandatory, and it is positional, meaning I don't have to type the parameter name, so long as I provide this scriptblock in position one. That contrasts with a parameter like, "InputObject," which isn't required, but if I choose to use it, it's named, meaning I have to type, "-InputObject" or at least some portion of it.
I can't simply stick this "psobject" in a particular position. So hopping back to our shell here, I can eliminate that entirely. Of course, there's aliases too. It's unusual for someone to type "where dash Object," so let's hop back over here and run, "getalias" or its alias "gal." And, wow, it makes it a little bit difficult to actually find a command. So we can scroll through here or perhaps let's see what type of data that output.
It does have a definition property. So why don't we just sort that on "definition." That way all of the aliases for "where-Object" fall in a particular location. We can see that instead of typing "where dash object," we could just type "where" or we could even just type a question mark. So that's the shortest possible way of abbreviating that command. Let's get rid of "where-Object" and just do that. Now how much can we abbreviate "get-Service"? Well, since we've got our alias list right here let's just scroll up to the Gs, see what kind of aliases we might have to find for "get-Service." Right here we see that it's "gsv." While we're here, we might as well take a look at the help for this.
And I see that the name parameter here, if I choose to use that, is optional. That means it's positional. Again, it's a good idea to verify that. Let's look at the full help and scroll down to the name parameters specifics. Yep, it tells me that although I don't have to provide the name parameter, if I choose to do so, I'll be putting a string into the first position. So that means back over here we can actually delete "-name" entirely and abbreviate this to "gsv." This is definitely not as easy to read as the original version of our command, but it's important to understand what's going on.
This string is being fed to the "-name" parameter even though I haven't typed "-name." This is an alias for "where-Object" and this filterscript is being passed to its "-filterscript" parameter even though I haven't typed the parameter name. So this does make PowerShell a little harder to approach. What you can do is if you're working with someone who's using this syntax, ask them to expand things back out into more of the long form where every single commandlet is completely spelled out, and every single parameter is listed with its parameter name.
As you get started, that makes it easier to figure out what goes with what. As you become more proficient at the Shell, you can start saving yourself time and a little bit of finger pain by beginning to abbreviate.