In my earlier post I explained about function in SharePoint. to take this a step forward I’m now looking at modules.

There are 4 types of modules.

  • Script modules
  • Binary Modules
  • Manifest Modules
  • Dynamic Modules

Script Modules

In my PowerShell scripts I try to include all my functions and the main script in two separate files. Script modules are quite good for this.

Binary modules

A dll contains the code that is available in the module.

Manifest modules

Manifest modules are modules that include a module manifest. This is a .psd1 file that looks like this:

#
# Module manifest for module ‘myManifest’
#
# Generated by: User01
#
# Generated on: 1/24/2012
#

@{

# Script module or binary module file associated with this manifest
# RootModule = ”

# Version number of this module.
ModuleVersion = ‘1.0’

# ID used to uniquely identify this module
GUID = ‘d0a9150d-b6a4-4b17-a325-e3a24fed0aa9’

# Author of this module
Author = ‘User01’

# Company or vendor of this module
CompanyName = ‘Unknown’

# Copyright statement for this module
Copyright = ‘(c) 2012 User01. All rights reserved.’

# Description of the functionality provided by this module
# Description = ”

# Minimum version of the Windows PowerShell engine required by this module
# PowerShellVersion = ”

# Name of the Windows PowerShell host required by this module
# PowerShellHostName = ”

# Minimum version of the Windows PowerShell host required by this module
# PowerShellHostVersion = ”

# Minimum version of the .NET Framework required by this module
# DotNetFrameworkVersion = ”

# Minimum version of the common language runtime (CLR) required by this module
# CLRVersion = ”

# Processor architecture (None, X86, Amd64) required by this module
# ProcessorArchitecture = ”

# Modules that must be imported into the global environment prior to importing this module
# RequiredModules = @()

# Assemblies that must be loaded prior to importing this module
# RequiredAssemblies = @()

# Script files (.ps1) that are run in the caller’s environment prior to importing this module
# ScriptsToProcess = @()

# Type files (.ps1xml) to be loaded when importing this module
# TypesToProcess = @()

# Format files (.ps1xml) to be loaded when importing this module
# FormatsToProcess = @()

# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
# NestedModules = @()

# Functions to export from this module
FunctionsToExport = ‘*’

# Cmdlets to export from this module
CmdletsToExport = ‘*’

# Variables to export from this module
VariablesToExport = ‘*’

# Aliases to export from this module
AliasesToExport = ‘*’

# List of all modules packaged with this module
# ModuleList = @()

# List of all files packaged with this module
# FileList = @()

# Private data to pass to the module specified in RootModule/ModuleToProcess
# PrivateData = ”

# HelpInfo URI of this module
# HelpInfoURI = ”

# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
# DefaultCommandPrefix = ”

}

 

Dynamic modules

Ok, these are the most tricky ones. Dynamic modules are modules that do not exist on disk.

These modules are created with the New-Module cmdlet and will only live during the period of the session.

new-module -scriptblock {function Hello {“Hello!”}}

I must say I haven’t really found a good use for this yet.

Easy … There are a couple of important things though to get it right.

 

Things to remember

The Module with the functions needs to be created in separate folder where the folder name is the same as the module name.

So you get something like:

MyScript.ps1

Modules (a folder)

Modules/MyFunctions  (a folder)

Modules/MyFunctions/MyFunctions.ps1

Then the variable $env:PSModulePath needs to be updated:

$path = Split-Path -parent $MyInvocation.MyCommand.Definition

if ($env:PSModulePath -notlike “*$path\Modules*”)

{

$env:PSModulePath += “;$path\Modules”

}

 

There is $PSScriptRoot which is a variable that gives the directory of the module.

Finally the module can be imported with the following command:

Import-Module -Name MyFunctions

 

If the $env:PSModulePath isn’t updated PowerShell will give the following error:

Import-Module : The specified module ‘MyFunctions.ps’ was not loaded because no valid module file was found in any module directory.

Avatar for Pieter Veenstra

By Pieter Veenstra

Business Applications Microsoft MVP working as the Head of Power Platform at Vantage 365. You can contact me using contact@sharepains.com

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from SharePains by Microsoft MVP Pieter Veenstra

Subscribe now to keep reading and get access to the full archive.

Continue reading