PowerShell Studio: Convert Functions into a Module
- Written by David Corrales
- Last Updated: 22 April 2016
- Created: 09 July 2014
- Hits: 11339
You may find at times you reuse various functions and constantly copy them into new scripts or find different variations of the same function are located in different scripts. You may come to the conclusion that those functions would better serve you in the form of a module where all your scripts can reference the functions in a centralized location. This is where the New Module From Functions feature comes in handy.
The New Module From Functions allows you to import functions from various ps1 scripts and merge them into a new script module. When you select this project from the menu it will ask you to select a file (if one not already open) and present you with the following dialog:
Let’s cover each field individually:
Enter the name you wish to give your new module.
Specify the folder where the module will be saved. The default is PowerShell Studio’s project directory.
Create Module Folder
This option creates a folder using the module’s name and places all the generated files within that folder. This folder will be created in the folder specified by the Location field.
Contains the list of files from which the selected functions with be extracted.
Use the Add File button to add ps1 scripts to the Source Files, in order to extract their functions.
Use the Remove File button to remove unnecessary files from the Source Files list. This can help de-clutter the functions list.
The functions section contains a node for each file and its respective list of functions that are declared in said file.
Use this list to check the functions you wish to import into the new module. If a function references another function, it will have a Referenced Functions folder icon below that contains a list of the all the referenced functions.
When you check a function in the list that has references, it will automatically check all the referenced functions as well.
You can check and un-check all the functions in the file by checking/un-checking the file’s node.
Once you have selected the functions and specified a module name you can press the Create Module button to generate the new module.
Note on duplicate functions:
In some instances you may have a duplicate function that is defined in multiple files. PowerShell Studio will compare these functions and if they are identical, it will only insert the function once. If the functions are different, PowerShell Studio will automatically rename the duplicate. A warning will be displayed in the Output window when this occurs:
The new module project will contain three files:
Module Name.psd1 – Contains the module’s manifest.
Module Name.psm1 – Contains all the imported functions and an Export-ModuleMember cmdlet statement listing all the imported functions.
You can remove any function you do not wish to be exported by the module here.
Module Name.Help.xml – Contains the PowerShell Xml Help file for the module, generated using the imported functions.
Now you have a complete PowerShell module with minimal effort on your part.