An Insider’s Guide to PowerShell Module Information

Summary: Microsoft Scripting Guy, Ed Wilson, discusses some of the great resources for learning about creating and using Windows PowerShell modules.

Microsoft Scripting Guy, Ed Wilson, is here. The Florida trip is nearly upon us. The Scripting Wife and I are busy packing and looking forward to a great week. We kick off the week in grand style—a Script Club meeting of the Charlotte Windows PowerShell User Group. We will be meeting in AP2 on the Charlotte Microsoft campus at 6:00 PM. If you are in the area we would love to see you. We then head to Pensacola for SQL Saturday, to Orlando for Microsoft TechEd, and then to Jacksonville for the Jacksonville IT Pro Camp. There are still seats available for the Jacksonville IT Pro Camp, so you should definitely check it out. I will be presenting in the afternoon, and there are some most excellent speakers who I am looking forward to hearing! I lived in Jacksonville for a couple of years, and there are some great things to do in the area. It is a great way to cap off an awesome week of learning following TechEd.

When I was speaking at the sold-out Atlanta TechStravaganga last weekend, during much of the question-and-answer portions of the presentations, I often answered a question with, “I have written an entire week of Hey, Scripting Guy! Blog posts about that subject.” At one point, Mark Schill interjected, “Ed’s written an entire week of blogs about nearly everything.”

Although it is not exactly true, there are 1,308 Hey, Scripting Guy! Blog posts about Windows PowerShell topics. One problem with having that much content is that people often do not know where to start. It used to be that you could use the Windows PowerShell tag, then select the Getting Started tag and begin reading. But now, even that filter returns nearly 300 Hey, Scripting Guy! Blog posts. At an average word count of 1500 words per blog, that is 450,000 words (the equivalent of two books the size of my Windows PowerShell 2.0 Best Practices). The results of the Windows PowerShell + Getting Started tags are shown in the image that follows.

It occurred to me that I need to create a guide to assist you to quickly find the Hey, Scripting Guy! Blog content you need. At a very high level, you should use the Study Guides from the Scripting Games, because they can help you find summary blogs about specific topics:

Don’t Write Scripts, Write PowerShell Modules Discusses how a Windows PowerShell module is more flexible and more powerful than a simple Windows PowerShell script. I provide an example of combining the functionality of several Windows PowerShell scripts into a single module. Specifically, I am discussing deploying and configuring Windows 7.

Don’t Write WMI Scripts, Use a PowerShell Module Discusses using a module that is written by Windows PowerShell MVP, Richard Siddaway, to reduce the need to write WMI scripts. There is no need to continue writing the same old scripts over and over again, when a perfectly good WMI module has already been written.

Creating a module: Practical examples

One of the best ways to learn about anything is to watch an expert perform the task. This is true whether one is talking about learning how to create hand cut dovetails or learning to create Windows PowerShell modules. The following sections provide links to several module projects. Begin with the unit conversion module, then proceed to the local user management module, and then follow up with the WMI module. Even if you are not fascinated by the technology that is used for the specific example, the techniques are valid. It is just as if you were specifically interested in cutting dovetails in cherry wood—you can still learn a lot about dovetails by watching someone cut them in oak wood.

The unit conversion module

In February 2010, I moved the Hey, Scripting Guy! Blog from five days a week to seven days a week. In honor of that occasion, I created a six part series that begin with a Windows PowerShell script that contains a collection of functions, and it emerges with an extremely useful module that performs dozens of unit conversions. I continue to use the Scripting Guys conversion module to this day. After you understand the basics of modules, you will want to go over this six-part module tutorial.

Conversion Module, Part 1 I take a Windows PowerShell script that contains a number of functions, and add them to a newly created module. I make basic changes to the functions, and then I install and test the newly created module. This blog is a great overview of the process of creating a module from an existing function.

Conversion Module, Part 2 I begin by adding descriptions to each of the functions in the module. I also add input and output parameters to the comment-based Help.

Conversion Module, Part 3 I create a new function called ConvertTo-Pounds. I discuss the design considerations for creating a function that accepts multiple inputs and multiple parameters.

Conversion Module, Part 4 I add a new function called ConvertTo-Liters. The blog discusses static methods from the System.Math class, in addition to the problem of working with multiple types of input.

Conversion Module, Part 5 I convert the output from the various functions so that they return a custom object instead of simply returning text. This change permits further work with the output without the requirement to parse text.

Conversion Module, Part 6 I talk about adding custom aliases for each of the functions in the module. In addition, I discuss using the Export-ModuleMember cmdlet.

The local user management module

The local user management module was developed and discussed in a series of four Hey, Scripting Guy! Blog posts:

The WMI module

The HSG WMI module was developed over six Hey, Scripting Guy! Blog posts. In each blog, I add additional functionality, and refinements.

A PowerShell WMI Helper Module Described Discusses the creation and the use of a WMI helper module, including several functions such as Get-WmiClasses with qualifiers, Get-WmiClassMethods, and Get-WmiClassProperties.

Query WMI Classes by Using PowerShell and Wildcard Patterns The fifth version of the HSG WMI module adds a very cool function that allows you to use wild card characters to find a WMI class and to automatically query the classes that match that wild card pattern. This is an exceptionally powerful way to explore WMI data and to find the appropriate WMI class to solve a specific need.

Module details

Checking for Module Dependencies in Windows PowerShell When using modules in a script, or even from the Windows PowerShell console, you need to know if the module exists. If the module is not available, you need to notify the user in an appropriate manner. The Get-MyModule function in this blog is a useful approach for dealing with this situation.

Specific modules

Use a Free PowerShell Module to Ease DNS Administration Chris Dent talks about his Windows PowerShell DNS module called DNS Shell. This powerful module can perform a number of tasks that include getting DNS zone information and creating DNS zones and records, in addition to a number of advanced tasks.

Use a Module to Simplify Your PowerShell Profile This blog talks about storing your Windows PowerShell profile in a module, or even in a series of modules. It offers specific recommendations, including group similar functionality into a module.

The WMI permanent event consumer module is discussed by Trevor Sullivan in two blog posts. He illustrates the use of a WMI module to ease the task of creating permanent event consumers.

Use a PowerShell Module to Easily Export Excel Data to CSV Jeremy Engel returns to the Hey, Scripting Guy! Blog to discuss his Microsoft Excel module. In this blog, he talks about two functions: Import-Excel and Export-Excel. This is a useful module that can simplify working with Windows PowerShell data and Microsoft Excel data.

As you can see, there is a lot of help on the Hey, Scripting Guy! Blog for creating and using Windows PowerShell modules. To be honest, I had forgotten about some of these posts. I hope you save this page and come back to it again and again. Join me tomorrow for more Windows PowerShell cool stuff.