Create PowerShell Script Modules and Module Manifests


To totally unlock this section you need to Log-in


Login

Let's say we need to create a simple module (in which are contained useful functions we have created) in Powershell. Let's start opening our .ps1 file which contains our beloved functions:

Create PowerShell Script Modules and Module Manifests

We want to create a module that contains these functions. There are several different types of modules, but what we’ll be creating is a “Script Module”. Modules sound like something really complicated, but script modules are actually simple.

Currently, we have the functions saved as a ps1 file which we dot-source to load the functions into memory, but we want to share this tool with others so it makes more sense to create a module out of it.

We are simply going to save the existing ps1 file as a psm1 file in a folder with the same name in our modules folder as shown in the following image:

Create PowerShell Script Modules and Module Manifests

It’s very important to have the folder and base name for the file the same. We've given it a generic name because we may want to add more functions to a new version of this same module in the future. We called it MrAuditTool .

At this point, it’s a script module. We can see what commands it contains:

Create PowerShell Script Modules and Module Manifests

The following image contains two important things to be aware of, first shown with the purple rectangle around it, if you have imported the module you are working with or it’s been imported automatically in PowerShell version 3 and changes are made to it, you will either have to remove and re-import it or you can simply add the -Force parameter to the Import-Module command.

The second item we want you to be aware of is if you don’t use standard verbs for your function names, you’ll receive the warning message shown in the following image. We've placed a red rectangle around the warning and the function that doesn’t use a standard verb (click on the image for a larger version which is easier to read):

Create PowerShell Script Modules and Module Manifests

View the output of Get-Verb to see a list of common verbs.

In addition to creating the .psm1 file that contains your module’s commands, you should also create a module manifest to describe its contents and system requirements.

The New-ModuleManifest cmdlet is used to create a module manifest. As shown below, in PowerShell version 3, the only field that you’re required to provide a value for is the path:

Create PowerShell Script Modules and Module Manifests

Although, if the RootModule is not set in the Module Manifest file, it won’t work and no commands at all will be exported. If you run into this problem, make sure this option is set in the psd1 module manifest file:

Create PowerShell Script Modules and Module Manifests

You could open the file with the ISE at this point and edit it manually, but we are going to delete this manifest file and create a new one with the options we want instead:

Create PowerShell Script Modules and Module Manifests

What’s really cool about the manifest file we just created is now it only exports the one New-UserAuditReport function since all of the others are helper functions:

Create PowerShell Script Modules and Module Manifests

There are other ways to export only specific commands in the psm1 module file instead of using this method in the psd1 module manifest file.