Today I have the pleasure of sharing another PowerGUI Script Editor Add-on with you that I recently finished and that I personally find extremely useful. This Add-on is all about defining PowerShell modules. It’s called the Module Management Add-on, and it adds the following commands to the PowerGUI Script Editor menu and toolbars:
- New Module – facilitates the creation of brand new modules from scratch by creating the module folder as well as the script module (psm1) file and the module manifest (psd1) file that defines the module, pre-populating as many relevant details in the manifest as possible such as the module name, description and GUID, the copyright date, your name, your company name, etc. in the appropriate places.
- New Module Manifest – facilitates the creation of manifest files for existing modules, pre-populating as many relevant details in the manifest as possible such as the module name, description and GUID, the copyright date, your name, your company name, and the complete listing of all files included in the module.
- Convert to Module – facilitates creating a new module from an entire script file or from the selected portion of a script file (this also generates the manifest and it is a really easy way to convert from PowerShell 1.0-style ps1 files that act as a function library but cannot be unloaded to PowerShell 2.0 modules that can be loaded and unloaded as needed).
Here’s a screenshot showing what the File menu looks like after you have installed this Add-on:
Also, this Add-on adds some of the more frequently used commands to the toolbar:
When you want to create a new module, you can either use the menu item or the toolbar button to create the new module, or simply press Ctrl+Shift+M. This displays the following dialog:
The author and company name are pulled directly from your Windows Registry, and the description and version fields are assigned generic default values. You can change these values however you like for your module, and the Add-on will remember the values (aside from name) for the next time you use this command. Also, you can use this dialog to create child modules by specifying a name with backslashes in it. The module that is generated will be placed in the WindowsPowerShell\Modules folder under your user Documents folder, with all files in a folder for the module. As soon as you enter a name and then click on OK, the module will be created and both the script module (psm1) file and the manifest (psd1) file will open in the Script Editor for editing, allowing you to add them to source control right away if you are using PowerGUI Pro with the source control feature. As an option, you can use this dialog to create a new module without a manifest by simply clearing all fields except for the Name field.
There may be cases where you already have a module that you have created, and you want to create a manifest for that module. Or perhaps you have a module you are already working with and you want to create additional manifests for that module (this allows users to work with an entire module or only portions of that module). In these cases, you can use the menu item to create the new manifest, or you can simply press Ctrl+Shift+N. This displays the following dialog:
Look familiar? It should. Creating a module manifest for an existing module is very similar to creating a new module because most of the details you provide in this dialog are stored in the module manifest. Just like when you create modules, you can modify these details that are provided by default and you can assign a name (with an optional relative path) for the module for which you are creating a manifest. Once the manifest is created, the Script Editor will open the manifest file automatically.
The other useful feature included in version 1.0 of the Module Management Add-on is the Convert to Module command. This can be invoked through the File menu or the toolbar button as well. You can either convert an entire file to a module or, if you have any text selected in the file, only the selected text will be used in the new module. Note that the original file and its contents are left intact during the conversion process. To perform the conversion, simply open the file you want to use for the conversion and select the text you want to use as the base for your module or don’t select anything if you want to convert the entire file. Then click on the Convert to Module command in the File menu or on the toolbar, and you will be presented with the following dialog:
Once more, this should look familiar. All operations for creating modules and manifest accept the same input values because they are all used in each operation. You simply provide the necessary details, or remove all fields except for the module name if you don’t want to use a manifest, and then click on OK for the module to be created and the appropriate files to be opened in the Script Editor for you.
These are all very useful features to have at hand, whether you are working on new modules in PowerShell 2.0 or creating PowerShell 2.0 modules from function libraries (ps1 files) that you created in PowerShell 1.0.
If this Add-on interests you, simply go to the Module Management Add-on page and follow the installation instructions provided there.
As with many other Add-ons I have created, this is an early version of the Add-on so I would love to get your feedback on it. Do you find it useful as is? How you would like to see it improved in a future release? Would you like to see a Module menu and/or toolbar with menu items to add functions and aliases? What else would you like to see for improved module management? I have ideas, but I don’t want to influence your feedback too much, so I’ll keep my ideas to myself and let you speak up about what else you would like to see in this Add-on or others.
As always, thanks for reading!
|Share this post:|
3 thoughts on “PowerShell modules made easy with PowerGUI®”
Very useful feature…
So here’s a question… Lets say you don’t want to import all the functions in one singular .psm1 file… Lets say that you hypothetically want to keep the functions in seperate .psm1 files. How do you adjust the manifest?
It’d certainly make updating the module easier down the line.
Great question, and there are several possible solutions.
What exactly do you want to accomplish? Do you want one module with all functions, but have those functions split out into multiple files so that multiple authors can modify different files at the same time? Do you want to have one master module that imports all functions and several nested submodules that could be imported instead for users who want only a subset of the functionality loaded? Or do you want to have multiple top-level modules each with a small function set? You can do each of these with modules.
It sounds to me like you just want to split out the functions into multiple files so that it is easier to manage updates by sharing the work with others. If that is the case, you simply copy each of the function sets (or individual functions if you prefer) into separate ps1 files and then you dot-source the ps1 files inside of your module’s psm1 file. No changes to the manifest are required for this to work, however it would be a good idea to add the individual files to the FileList property in the manifest. Your ps1 files can be in the same top-level module folder or you can break them up into subfolders if you want to organize them differently, and you can use $PSScriptRoot to identify the module’s root folder when it is imported in your dot-source statements so that you can use relative paths that work regardless of the location of the module. Does that make sense?
If you have additional questions, don’t hesitate to ask.