Share via


Three Steps to PowerShell Module Success

Your eyes haven't deceived you, my friend... create yourself a PowerShell module in just three easy steps!

Here goes:

  1. Determine a Module Path
  2. Create the Module Folder
  3. Save your Functions to a PSM1 File

 

And, that's it. No, really! Well, you also need to have written the functions for the module, but, apart from that, it really is that easy.

Why use modules? If you have a number of related functions that you want to group together, if you want to seamlessley load those functions into your PowerShell hosts, if you want to easily share your work with the world, then, sir, modules are for you!

Now, let's take a look at those steps in more detail...

 

1. Determine a Module Path

We need to know where to create our module folder in step 2. To do that just run this and analyse the results:

$env:psmodulepath.split(";")

Here's some sample output:

 

Each line is a location that PowerShell checks for modules to load. For our own modules we should use our own user profile path. In the above example that's:

C:\Users\Administrator.NINJA\Documents\WindowsPowerShell\Modules

 

2. Create the Module Folder

Now we need to create a folder for our module. The name of the folder will be the name of the module. Avoid spaces and other weirdness. For example, I'm going to create a module called RodcManagement, so I create a folder called, er, RodcManagement.

 

3. Save your Functions to a PSM1 File

Right, now create a text file with the same name as your module folder. NB- don't use a different name or bad things will happen....

Rename that text file to a psm1 file. Edit the psm1 file and add your pre-canned functions to it. Save the file. Admire your handy work.

 

Hmmm...

 

Sweet!

 

Ok, before I finish, I'll set you on the path of module refinement. You have the option create a Module Manifest to describe the contents of your module and add some interesting complexity. Here's an example of how to generate a simple manifest file:

New-ModuleManifest -Path "C:\Users\ianfarr\Documents\WindowsPowerShell\Modules\RodcManagement\RodcManagement.psd1" -Author "Ian Farr" -CompanyName "Microsoft" -ModuleVersion "1.0" -Description "Module for securing and managing RODC" -PowerShellVersion "3.0" -RootModule "RodcManagement.psm1"

The manifest is a psd1 file which PowerShell, when loading a module, will read in before the psm1 file. One great advantage of a manifest is that comment based hep for your functions becomes available.

Over to you...