With each new version of Microsoft PowerShell, now at version 5.0, comes a new set of commandlets to increase the scripting capabilities for deploying and managing the various Windows Server roles and features. Now, numbering in the thousands, commandlets have organized into ‘modules’. PowerShell modules are an efficient way to extend the capabilities of PowerShell to support new roles and features as well as provide a means for the user to write and easily use their own custom commandlets, known as functions.
Using PowerShell Modules
Most modules are installed as part of the installation of the role or feature. For instance, the set of commandlets used to manage Active Directory are contained in the Remote Server Administration Tools. In addition to the Remote Server Administration Tools, modules are also available for download from the Microsoft Technet Gallery. Common modules available for download are NTFSSecurity, WindowsAzure and WindowsInstaller, just to name a few.
To be able to access the commandlets with a downloaded module, the module must be loaded in one of the two locations specified in the %PSModulePath% environment variable, which are C:\Users\<username>\Documents\PowerShell\Modules and C:\ProgramFiles\Microsoft\PowerShell\Modules. You’ll need to create a folder inside the Modules folder that is exactly the same name as the PowerShell module name. Within that folder copy the module files. Once the module has been copied to the proper location the module must be ‘imported’ into your PowerShell session by using the Import-Module <ModuleName> commandlet. The import process only imports the module for the duration of your PowerShell session so you will need to import the module each time you start a new PowerShell session. For commonly used modules, it is possible to create a PowerShell profile that can automatically load modules when a new PowerShell session is started. Profiles will be covered in a future article.
How to Create Custom PowerShell Modules
As was mentioned at the beginning of the article, it is possible to create your own modules and import them as well. In this case, a module is simply a function that has been saved to %PSModulePath% environment variable location. While a complete explanation of a PowerShell function is beyond the scope of this article, suffice it to say that a function is simply a PowerShell script that contains a set of PowerShell commands that together perform some useful function, such as retrieving recent, specific events from multiple servers and outputting those events to a file. The script is then ‘wrapped’ inside a function block that defines the function name and input parameters. This function is then saved as a module using the .psm1 file extension, in the PSModulePath location. To use the function within the module, simply import the module and the function will be available for use just as is any other native PowerShell command. In fact, once imported, Intellisense will contain the name of your function and its input parameters.
One last note about using your own custom modules. It’s important to bear in mind that the Import-Module command loads your module into memory and will ONLY use the in-memory version of your module. If, during your PowerShell session, you import your module and then modify the contents of that module within the same session, you must reload your module into your session to have your changes applied to your active session. To do this execute the Remove-Module <modulename> and then reimport your module using Import-Module <modulename>.
The Microsoft Official Course 10961 – Automating Administration with PowerShell provides comprehensive instructions on functions and modules and how to convert your scripts to modules.