PowerShell Studio 2014: Convert Functions into a Module

July 9th, 2014 by David Corrales
Last updated on July 9th, 2014

 

PowerShell Studio 2014 added a new project feature: New Module From Functions.

image

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:

SNAGHTML330af9

Let’s cover each field individually:

Module Name

Enter the name you wish to give your new module.

Location

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.

Source Files

Contains the list of files from which the selected functions with be extracted.

Add File

Use the Add File button to add ps1 scripts to the Source Files, in order to extract their functions.

SNAGHTML3d6f18

Remove File

Use the Remove File button to remove unnecessary files from the Source Files list. This can help de-clutter the functions list.

Functions

The functions section contains a node for each file and its respective list of functions that are declared in said file.

image

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.

image

Tip
When you check a function in the list that has references, it will automatically check all the referenced functions as well.

Tip You can check and un-check all the functions in the file by checking/un-checking the file’s node.

 

Create Module

Once you have selected the functions and specified a module name you can press the Create Module button to generate the new module.

image

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:

image

 

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.

image

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.

 

 
[Google+]   [Facebook]   [LinkedIn]   [StumbleUpon]   [Digg]   [Reddit]   [Google Bookmark]  

Tags: , , , ,