Jeff Hicks is a PowerShell MVP, all-around smart guy — and he’s apparently doing community service for something, because he agreed to share his expertise with us.
As he told us, "I had nothing else to do tonight, so I figured I might as well do this. I was literally just going to sit in the corner and stare at the wall until this came up."
Those of you who watch my show know I’m a huge fan of PowerShell. Got a big old crush.
I used to do the PowerShell tip of the week until I ran out of things that I knew about PowerShell. Which means there were only like three weeks of PowerShell tips.
It was very short-lived. But you know, Jeff Hicks and I still travel in the same circles, speak at the same conferences. Our paths have crossed. We’ve chatted about PowerShell. So I wanted to bring him on and just kind of get some honest to goodness PowerShell talent in here to talk about some stuff.
Talking with Jeff Hicks
Klindt: The thing that got me real excited recently was the week of blog posts by several of my favorite PowerShell experts. So tell everybody about How To Write PowerShell Functions in a Week of Blog Posts.
Hicks: A couple of weeks ago I thought I'd try a kind of a social media experiment. So I contacted some other PowerShell people in the community like Mike Robbins and June Blender. And we decided to write a bunch of blog articles on a central topic and schedule them one a day to build up some interest in PowerShell — maybe draw some traffic to our sites and maybe even grow some Twitter followers. So it was as much a social media experiment as it was a PowerShell experiment.
We came up with this idea to write about advanced PowerShell functions. We each picked a topic. Actually, I think Adam Bertram came up with the list and assigned things to us.
And then we just wrote our topics, scheduled them once a week, used a hash tag to schedule them all out, and then, when we were all done, I put together a PDF and some other e-book formats of all the posts, which are also available. People seemed to like it and a lot of people said, “Well, hey, how come you didn’t tell me about this? I want to write next time.”
So I’m sure the next time — because I’m going to be very surprised if there’s not a next time – I’m sure the next time that we do this kind of PS blog week thing there’ll be more people involved and we’ll pick another topic.
Klindt: I thought it was a great thing. It was really well done. I really liked it. It's a great way to break a topic up into bite-size chunks and get it out there. As a SharePoint guy, I was thrust into PowerShell. And I won’t bore everybody with telling the story again but I went in kicking and screaming. I was not happy. And I don’t think I’ve ever told you this story.
The very first big speaking gig that I ever got was on SharePoint 2003’s command link tool. It was called STSADM. So it was, you know, the crappy old command link tools that were …
Hicks: Yeah. You love that tool.
My Favorite Tool
Klindt: Right. So the very first speaking gigs at TechEd 10 years ago, STSADM. First book chapter I ever got published, STSADM. First magazine article I ever got published, STSADM. First tattoo – no.
That’s a different deal, but I was heavily – you know, I was a big deal because of STSADM and then I went out to Redmond to find out about SharePoint 2010. And they were like, “Hey, we’ve got great news. It’s all PowerShell.”
Now I remember just wanting to pick up my chair and throw it at the guy because I had a lot invested in STSADM but, frankly, I’m not smart enough for PowerShell. So I went through all the different stages of grief – you know, the bargaining and all that kind of stuff, and came out of it a better man eventually a couple of months later. But I think there’s a lot of SharePoint administrators and SharePoint developers that are in the same boat as I am who still think PowerShell is kind of scary and kind of intimidating.
And I think, you know, we kind of get enough PowerShell to do the thing we want and then we kind of stop and that’s why I thought the week of blog posts was great because you all know what you’re talking about. It’s kind of the next step for me: following good practices and writing functions.
And, you know, like Don Jones likes to say, “Don’t be the tool user. Be the toolmaker." So talk about that — what a function is, how we make it. Get everyone excited about it.
Hicks: I think it’s kind of funny that you mention STSADM because I tell people the great thing about the SharePoint cmdlets is you no longer have to use STSADM, which was an awful tool to try to do anything with.
Klindt: Oh, I think awful is awfully harsh. So it was a little awful.
I'm Not Afraid
Hicks: Anyway, so the whole point about PowerShell — for a lot of people who are new to PowerShell — is that it kind of puts them off. They think that they have to learn this arcane, Perl-like scripting language and that’s really not the case.
So you’re going to start by – we can talk about SharePoint. I mean, I don’t have the SharePoint cmdlets handy. I’m not even sure I could name any off the top of my head.
I’ve kind of dinked around with them a couple of years ago. So you’ve got some SharePoint cmdlets and you load them up in your PowerShell session and you say, “You know what? I want to run this SharePoint cmdlet to do X.” Well, you can type that command. Let’s say it’s “get library” or “give me a SharePoint cmdlet name.”
Klindt: So like “get-spsite”?
Hicks: Okay. “Get-spsite.” So I can do “get-spsite” and then, with whatever parameters I want to modify the behavior of that cmdlet, I can just type that interactively in the console and I get the results that I want.
Let’s say then that I want, though, to – I’m going to do something repeatedly with that. I want to do “get-spsite” and I want to use a certain set of parameters and then I want to export that information to a CSV file, let’s say, or X amount.
Let’s do X amount. That’d be better. So now, instead of having to type that long command – and it can be a one-line PowerShell command that would get all the SharePoint sites with whatever criteria you wanted and then export it to an XML file -- you could build a PowerShell function which takes the command, you wrap it in a function keyword, the name of your function in a set of curly braces with a script block, and then you can stick all your commands in there.
That function then becomes like a little mini cmdlet. So you could say maybe -- or maybe you call it “get-mysite.” You then have that function.
Now you can type “get-mysite” instead of having to type all that long command -- the “get-spsite, blah, blah, blah, pipe to export CLI, XML” or whatever you wanted to do -- now you have a command that you have built that meets your needs that’s your tool and you just have to type “get my site.” If you want, you can create an alias for that – “GMS.” So you can be really lazy.
Klindt: I openly mock people that use aliases but, since you’re smarter than me, I’ll let you go.
What's in a Name?
Hicks: Aliases are fine. This is nothing about PowerShell. PowerShell can be used in two ways. Well, actually, I’m going to tell this story because I love this story.
I use this when I teach PowerShell for people in training classes. PowerShell, if you remember Saturday Night Live back in the ‘90s, they used to do this commercial parody for Shimmer. It was a delicious dessert topping and a fabulous floor wax. So PowerShell is like that. It’s a delicious dessert topping and a fabulous floor wax.
It's a really cool interactive shell and it’s also pretty powerful and easy-to-learn scripting language. So you can have the best of both worlds.
Now, some people will do nothing but run interactive commands. They’ll be the tool users. They’ll run the commands, you know, get service, get-spsite or they’ll run the scripts and modules and functions that you build. Then you’ll have people who will just be creating their scripts and functions and modules and graphical scripts sitting on top of PowerShell. So PowerShell gives you the best of both worlds.
Now for aliases. When you’re working interactively in the console the goal should be efficiency. You don’t have to do a lot of typing. First off, PowerShell has tab completion.
I don’t know how many times I have to drill, when I’m teaching a class, into students, “Don’t type the full command in. –get-s, hit tab and tab complete to get to the command that you want.”
So you don’t even have to use aliases but, if you want to use an alias, if you know GSV, for example, is an alias for “get-service,” and, you know, hey, I just need to get a list of services. I’m typing it interactively. I can just bang out GSV probably even faster than I do “get-service” and do tab completion. There’s nothing wrong with that because you know what GSV means ‘cause you typed it.
This throws people off and it's the source of the controversy or whatever. If you write a script or a function and you put that GSV command in your function it’ll work because PowerShell will know what that is. But if someone were to look at that code that you wrote in PowerShell they’ll go, “What is GSV?”
And it takes them some time to figure that out, whereas if they saw in the script “–get-service,” you know, that’s kind of meaningful.
Now, one of the things that kind of cracks me up about the SharePoint cmdlets is just some of the names are just enormously long and complex, right?
But at least they’re meaningful. I’ll give them that. So if I were to see a SharePoint function or script and it used aliases for those cmdlets, I would be totally lost as to what it did unless I took the time to go and unravel what those meant. But if you gave me a script that you wrote that was built on SharePoint or the SharePoint cmdlets and you used the full cmdlet names with the full parameter names, even if I knew nothing about SharePoint or the cmdlets, I’d have a pretty good guess about what it is that your script was doing.
So using aliases interactively — go for it. The more you can do, the faster you can get it out, that’s fine. But I don’t want to see that in the best practices in anything that you publish either for yourself, for someone else or even if you post online or in forms.
You know, Twitter’s kind of a special case because you’re limited anyway. So, yeah, I’ll grant you there are some aliases but that’s one of my pet peeves.
People will post – you know, they’ll write a blog post and they’ll post their script and it’s the most – they’re using every single alias and some of them are really cryptic. And, to me, it looks like Perl because it feels incredibly cryptic.
And I think when people look at PowerShell, especially when it uses a lot of aliases or they’ll start using the .net framework where they could just as easily use a cmdlet instead of using the date/time – instead of using “get-date,” they’ll use the date/time class. Now, you’re just showing off as far as I’m concerned.
There are some aliases where the question mark is for a “where” object and the percent symbol is for each object but if you were to see that in a script, it’s incredibly cryptic. And my concern with it is that it just turns people off or worse they think, “Ah, I can’t learn PowerShell. It’s like programming. It’s too hard.”
Tab, Tab, Tab
Klindt: Ok, so tab completion is great to keep you from mistyping the name of the cmdlet because you don't have to spell it out if you tab complete it.
Hicks: That’s exactly true.
Klindt: And the other great thing -- NT Windows has had tab completion since the dawn of time but one thing that PowerShell brought that kind of upped the game is the tab complete on the parameter names. And I think that is just amazing because I cannot always remember the parameter that I want to use but I can -- tab, tab, tab, tab, tab. Love it.
Hicks: And if you do it in the ISE you get the IntelliSense popup. Do you know you get IntelliSense for parameter values? Do you know that?
Klindt: Probably not.
Hicks: So if you type, “get-service” -- let me make sure here that I’m not lying here. If you type “get-service,” face, and hit tab you will then -- it’ll then cycle through all of the services because the first parameter for get service which is positional is the name of the service.
And so PowerShell is smart enough to know that, “Oh, you must want the service,” and so it will cycle through but it will also work for parameters that have validation sets. So if a parameter can take, you know, value X, Y, or Z, so, you know, my command, parameter space, and hit the tab, it will cycle through X, Y, and Z.
Klindt: You've mentioned ISE and IDE. Can you explain what they stand for?
Hicks: So the ISE is the PowerShell integrated scripting editor I think is what it stands for. And IDE is a term for integrated development environment. So like Sapien’s PowerShell studio is in IDE because it’s full featured. It’s got all sorts of bells and whistles to make it easier for you to develop a script.
The ISE – the PowerShell ISE with chips on client operating systems, which is the only place you really need it, it’s an optional feature for servers, no one should really have it installed on the server but the ISE is designed for people who want to write, you know, simple scripts.
And simple can still be, you know, a thousand lines but, at some point, people in their PowerShell experience will kind of outgrow the ISE. You’re going to start running into memory limitations. Someone was mentioning – I can’t remember where I read it – he had like a several-thousand-line PowerShell script and the ISE just took forever to parse it.
And I said, “Well, yeah, you’re too big. First of all, you need to modularize and probably turn some of your three- or five-thousand-line script into a set of functions and make it reusable or you basically have outgrown the ISE. And what you’re trying to do is much more complex and advanced. And you now need a bigger tool set."
We Need a Bigger Blog
We're just getting started. There's a lot of stuff in Podcast 243. So if you're tired of reading, watch the podcast or listen on iTunes. The time stamps will link to the location of the content.
- 40:29 Learn about the PowerShell Summit (even though it is sold out and you cannot attend).
- 51:29 Looking for more PowerShell content? Jeff Hick writes for Petri.com and shares some other essential resources.
- 53:57 When Microsoft first released the fitness Band, you needed a smartphone to get it all set up. Now Microsoft has released a Band app for Windows that lets you hook up your band up to your PC with a USB cable.
- 59:40 I’m a big fan of Windows Phone, but I have hated the last two Windows 10 beta releases. Here's how you can turn the beta versions back with the Windows Phone Recovery Tool.
- 1:01:50 Shameless Self-Promotion: upcoming events and conferences (You still have a chance to win those AvePoint RED Party tickets. You have to leave a comment so that your name shows up so I know who you are. You can talk about how dreamy I am, my movie star good looks, the rich baritone of my voice – any of these are acceptable things to talk about.)