Two Cool PowerShell Functions

Doctor Scripto

Summary: Ed Wilson, Microsoft Scripting Guy, talks about a pair of cool Windows PowerShell functions to add to your profile.

Microsoft Scripting Guy, Ed Wilson, is here. At times, it seems like a Windows PowerShell profile is like a filing cabinet. Whether you have good organizational skills or you organize in a more ad-hoc, random, fashion is a matter of choice—as long as your organization is effective.

Today, I want to share a pair of Windows PowerShell functions that are in my Windows PowerShell profile. I believe that I copied them when June Blender and I team-taught a Windows PowerShell class for documentation writers. I think I got them from her. If not, they are cool enough that I don’t think she would feel insulted by my ascribing them to her.

Actually, I seldom copy code from other people. Instead, I write most of the stuff I use on my own. But these two functions (and now I am certain that I got them from her) were pretty cool, and they were something I had not thought about doing. That I did not document “copied from June B.” is probably because I jotted them down quickly while she was teaching a particular module in our course. In fact, it may not actually be her actual code, but maybe more suggested by her. Anyway, I do need to give her credit for her idea, which is rather cool.

The first function is the best. It gets the forward link from Windows PowerShell cmdlet Help to the online location of the Help content. Unfortunately, this does not work for the About* content, rather only for the cmdlet Help. This is really a shame, but that is how the About* content was actually implemented in Windows PowerShell 1.0.

So, what can I do with the forward link to online Help about a Windows PowerShell cmdlet? Well, if I was clever, I could write a script that would search a Word document and add a hyperlink every time I used a standard Windows PowerShell cmdlet, and the link would go to the online Help documentation. Pretty cool—and not all that difficult to accomplish.

To make it really cool, I would need a different “style” for code so I could only apply the hyperlinks to “normal” text. One of these days, I may actually write that script. The function relies on the fact that the Get-Command cmdlet retrieves the Helpuri property from each Windows PowerShell cmdlet. This even works when I use aliases such as gcm for Get-Command and gsv for Get-Service. This is shown here:

PS C:\> (gcm gsv).HelpUri

http://go.microsoft.com/fwlink/p/?linkid=290503

For now, here is the Get-ForwardLink function that I have in my profile:

Function Get-ForwardLink

{

 Param($cmdletName)

 # Get-WebPage -url (Get-CmdletFwLink get-process)

 (Get-Command $cmdletName).helpuri

}

The next function is called Get-WebPage. It takes a URL and opens the associated web page. This would be useful in a script that parsed Word documents and looked for 404 error messages.

The function is rather simple. It takes a URL, and then opens the URL by calling the Open method from the Shell.Application com object:

Function Get-WebPage

{

 Param($url)

 # Get-WebPage -url (Get-CmdletFwLink get-process)

 (New-Object -ComObject shell.application).open($url)

}

As you know, I nearly always create an alias for my functions, and this is no exception. Here are the two aliases:

Set-Alias -Name gfl -Value Get-ForwardLink | out-null

Set-Alias -Name gwp -Value Get-WebPage | out-null

The cool thing is that I can combine the two functions, and therefore open the online cmdlet Help from my Windows PowerShell console. This is shown here:

PS C:\> gfl gcm

http://go.microsoft.com/fwlink/p/?linkid=289583

PS C:\> gwp -url (gfl gcm)

PS C:\>

As shown here, I can see my online Help for Get-Command:

Image of command output

Now, of course, this example was just for fun. This is because Windows PowerShell Help already has an Online switch that will do the same thing:

PS C:\> get-help gcm -Online

PS C:\>

Profile Week will continue tomorrow when I will talk about more cool stuff.

I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at scripter@microsoft.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.

Ed Wilson, Microsoft Scripting Guy

0 comments

Discussion is closed.

Feedback usabilla icon