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:
All about modules
So, without further ado, here is a guide to Hey, Scripting Guy! Blog posts that talk about modules.
Beginning with modules
I began 2010 with Module Week. This week incorporated some text from my book, Windows PowerShell 2.0 Best Practices (published by Microsoft Press).
Tell Me About Modules in Windows PowerShell 2.0 Provides an introduction about using the Get-Module and the Import-Module cmdlets. The blog also discusses how to locate and load modules.
How Can I Install Windows PowerShell Modules on Multiple Users’ Computers? Talks about what is required to install a Windows PowerShell module. It includes the Copy-Modules script that automates this process. The blog also talks about the psModulePath environmental variable.
How Do I Work with Windows PowerShell Module Paths? Talks about creating a $modulepath variable and creating a Windows PowerShell drive to ease module management.
Can You Give All the Steps for Creating, Installing, and Using Windows PowerShell Modules? (After you have read the three previous blogs, you will want to review this one.) This blog explores creating a function, adding it to a module, and then importing the module. This is a great summary of the process. In fact, at the end of the blog, the seven steps to create and use modules are detailed.
Simplify Desktop Configuration by Using a Shared PowerShell Module Discusses placing a specific module in a shared location to simplify access. It uses the ConversionModuleV6 module as an example.
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.
Combine PowerShell Modules to Avoid Writing Scripts Explores using Windows PowerShell modules to save time and to reduce scripting overhead. Specifically, I talk about using the Windows 7 Library module from CodePlex as an example.
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:
- Use Parameter Sets to Simplify PowerShell Commands
This is a great technique, and it is the way that many Windows PowerShell cmdlets are actually written.
- Naming and Designing Advanced PowerShell Functions
These functions make up the local user management module.
- Use a Free PowerShell Module to Work with Local Accounts
- Use a Module to Simplify Scripting Local Users and Groups
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.
Use PowerShell to Easily Find the Key Property of a WMI Class Adds a function called Get-WmiKey that is useful when one needs to use the WMI type accelerator.
Use a PowerShell Function to Get WMI Key Property Values Adds a function called Get-WmiKeyValue, which returns the values of property keys from a specified WMI class.
Easily Filter Empty WMI Properties in PowerShell Adds a filter called HasWmiValue that is useful because it filters out property values that are empty or null. This makes exploring detailed WMI data very easy.
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.
Modify PowerShell Comment-Based Help to Display Function Use Adds aliases and other clean-up details to the WMI module.
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.
Learn How to Load and Use PowerShell Snap-ins Windows PowerShell MVP, Tome Tanasovski, discusses the differences between Import-Module, and Add-PSSnapin. He also covers the importance of a module manifest.
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.
Clean Up Your PowerShell ISE Profile by Using a Module This module includes a number of useful functions including Backup-Profile and Add-Help. It also discusses how to use a module to clean up a complex Windows PowerShell profile.
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 Work with WMI Permanent Events
- Use the PowerShell WMI Event Module to Quickly Monitor Events
Use the PowerShell DHCP Module to Simplify DHCP Management Jeremy Engel discusses creating the DHCP module, in addition to providing tips and tricks for its use.
Use PowerShell to Create VHDs for Hyper-V Windows PowerShell MVP, Marco Shaw, discusses using the Hyper-V module.
The BSONPosh module was written by Windows PowerShell MVP Brandon Shell, and it consists of a number of extremely useful functions. I talked about this module in two Hey, Scripting Guy! Blog posts:
- Use the BSonPoSH PowerShell Module to Simplify AD Tasks
- Use the BSonPoSH PowerShell Module to Check AD Replication
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.
Using Windows PowerShell to manage WSUS Boe Prox wrote an entire series of blogs about this topic. In the following blogs, he discusses using the PoshWSUS module:
- Introduction to PoshWSUS, a Free PowerShell Module to Manage WSUS
- Use the Free PoshWSUS PowerShell Module for WSUS Administrative Work Discusses installing and using the module, including using it for client and group management, update administration, reporting, and other functionality.
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.
I invite you to follow me on Twitter and Facebook. If you have any questions, send email to me at email@example.com, or post your questions on the Official Scripting Guys Forum. See you tomorrow. Until then, peace.
Ed Wilson, Microsoft Scripting Guy