All posts by Jeffery Hicks

Friday Fun: A PowerShell Macro

Today’s Friday Fun is a little different in that it showcases two things I use almost every day: Microsoft Word and PowerShell. I am writing new articles and material almost daily and of course very often the content is PowerShell related. Usually I use the blog post template in Word to make it easier to upload. This works great because I can insert links in the Word document and they will be maintained when copied to WordPress. One of the steps I’ve started taking in my writing is to include a link to online help for a cmdlet.

For example, if I am writing about Get-WinEvent I’ll include a link on the cmdlet name. Of course I’m not going to manually get the link, copy it and create the hyperlink in Word. So I created a Word macro that calls a PowerShell script to get the online link and insert it as a hyperlink. Here’s how this all works.

There are a few ways to get the online help link for a given cmdlet. You could retrieve the HelpUri with Get-Command.

Or you can retrieve it using Get-Help.

As you can see the links are different, even though the online content is very similar. But since the latter is what you would get if you ran Get-Help Get-Service –online, I decided to go with that. I put together a simple script.

The function writes the link to the pipeline if found, otherwise it writes $Null. I’ll explain why in a moment.

With this script, I now turn to Word and created this macro.

I couldn’t find a way to capture the output of the PowerShell command so I ended up creating a temporary file that contains the link. If nothing was found then the file will have a 0 length. I found this easier because the macro reads the file and saves the contents to a variable, link. If the length of link is > 0 then Word inserts the hyperlink, complete with a constructed screen tip.

I stored the macro in Normal.dot so I always have it available.

I even gave it a keyboard shortcut under File – Options –Customize Ribbon

Finding ways to automate the dreary tasks from my day is very rewarding, plus I almost always learn something new. Hopefully you did to.

Friday Fun: I Can Run that Command in 3 Letters

Large Blog ImageIf you have been using PowerShell for any length of time, I’m sure you are familiar with aliases. An alias is an alternative name to a PowerShell cmdlet. They are intended to serve as transition aids (like dir and ls) and as a means to keep interactive typing to a minimum. Although with tab completion, if you can get in the habit, perhaps this isn’t as big a deal. However, there are a number of aliases you might want consider creating if, like me, you spend a lot of time at a PowerShell prompt.

I still use Notepad, often as a temporary buffer. I’m too lazy to type “notepad” at a prompt. So I have this:

I use Set-Alias instead of New-Alias so that if I am trying to define something that already exists I won’t get any errors. You can also make your aliases Read-Only so you don’t accidently change them.

Another one that I’ve started using is an alternative to Select for Select-Object.

The Name and Value parameters are positional. Like you, I use a number of Office products.

Here’s one that might escape my younger readers.

I have a separate script file that defines all of my aliases which is dot-sourced in my profile script. I use a few Sysinternals utilities and while I could modify %PATH% it is just as easy to create a few aliases.

My other shortcuts are helper (cheater?) functions like these where the name is more like an alias.

As you can see, I’m using PowerShell aliases for more than cmdlets. What kind of shortcuts and aliases have you come up with?

Chicago – My Kind of Town

I grew up in Northwest Indiana so naturally Chicago played a big role. I love Chicago and if I had to pick a major city to live, this would be it. So I’m very excited to be heading there for Ignite. I’m looking forward to catching up with old friends and meet some new ones. So in between all of the awesome sessions, I hope you’ll look for me at my few scheduled “public” appearances. All times are CENTRAL.

Tuesday
I will be making an official appearance at the Pluralsight booth (#350) from 11:00 to 12:00. I’ll be happy to answer a few questions, sign things or whatever. In the evening I’ll be at the PowerShell Happy Hour and then on to the Petri Meet-n-Greet.

Wednesday
I will be at the Scripting Guys booth in the Expo Hall from 10:00AM to 10:30AM. Although I will likely be hanging around a bit longer. At least until 11:30 when I will be part of a panel discussion on PowerShell to be held in the Lounge B theater in the Expo Hall.

And I’ll wrap up my trip by joining the festivities with the Pluralsight party.

Otherwise, I’ll be wandering around chatting with people and trying to squeeze in a few sessions. Hope to see you in the Windy City.