Creating Script Modules

by Sep 15, 2009

In PowerShell v.2, there is a new feature called "module," which is a file with the extension .psm1 and behaves almost exactly like a script except for two major differences. Before you can use the functions defined in a module, you will need to load the module using import-module. Secondly, modules can determine which functions should be public and which should be hidden (for internal purposes). Here is a quick example of a very simple module:

$internal = 1
$external = 2
function Test-Function1 {
"I am function 1"

function Test-Function2 {
"I am function 2"

Export-ModuleMember function Test-Function1 -Variable external

You should save this file as example.psm1 and then import it like this:

Import-Module path_to_module\example.ps1

Once complete, you can now call Test-Function1 and $external, but you cannot access Test-Function2 and $internal because they were not explicitly published.

Twitter This Tip! ReTweet this Tip!