PowerShell Export-ModuleMember vs Export keys in manifest

28 Mar

In PowerShell there are two ways to export functions, cmdlets, variables and aliases from a script module file for use by the calling context:
1. By using Export-ModuleMember and specify what resources need to be exported like:
Export-ModuleMember -Function or Export-ModuleMember -Variable

2. By using a module manifest and specifiy what resources need to be exported like:
@{
….
FunctionsToExport = ‘ VariablesToExport = ”
….
}

By seeing this I got a little confused on which method to use and why: Export-ModuleMember or the manifest way, so it was time to dig into this matter.
By creating a small module, a manifest and a script file the different options can be tested.

Module (psm1)

In the module three functions, two with an alias and two variables are defined and exported:

$msgText = 'Hello world!'
$anotherVariable = 'more here'

function Say-HelloWorld() { Write-Host $msgText }
function Calc-Numbers([int] $a,[int] $b) { $a + $b }
function I-Am-Private() { Write-Host "private" }

Set-Alias Add Calc-Numbers
Set-Alias Hello Say-HelloWorld

Export-ModuleMember -Function Say-HelloWorld, Calc-Numbers
Export-ModuleMember -Variable msgText, anotherVariable
Export-ModuleMember -Alias Hello, Add

Manifest (psd1)

The export variables of the manifest are (default) set to ‘*’:

# 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 = '*'

Script (ps1)

The script file imports the module and displays information about the module. This is an excellent way to test what resources are exported:

Import-Module ExportFunctions -Force
Get-Module -Name ExportFunctions | fl

Tests

Running this script results in the following output:

As can be seen two functions, the aliases and variables are exported as defined in Export-ModuleMember in the module.

The Export-ModuleMembers lines are now removed to test the default settings in the manifest file (all ‘*’ for the export keys). Running the same script now results in:

All the functions are exported, but none of the alias or variables. At first sight this seems a bit strange, because the generated comment of the export keys of the manifest say ‘ to export from this module’. This seems only the case for the functions.

TechNet documentation for FunctionsToExport key:

Specifies the functions that the module exports (wildcard characters are permitted) to the caller’s session state. By default, all functions are exported. You can use this key to restrict the functions that are exported by the module…

And the TechNet documentation for VariableToExport key:

Specifies the variables that the module exports (wildcard characters are permitted) to the caller’s session state. By default, all variables are exported. You can use this key to restrict the variables that are exported by the module…

The statement don’t differ, but the behavior is.

The ‘*’ at the FunctionsToExport key means the restriction is:

If Export-ModuleMember -Function in the module is used, the functions listed there are exported:
In module:

Export-ModuleMember -Function Say-HelloWorld, Calc-Numbers

In manifest:

FunctionsToExport = '*'

Result:

If Export-ModuleMember -Function is NOT used in the module and in manifest ‘*’ is used all functions are exported:

The ‘*’ at the VariablesToExport key means: By default none of the variables are exported, unless variables are exported by the Export-ModuleMember in the module.

In module: No Export-ModuleMember -Variable
In manifest:

VariablesToExport = '*'

Result:

In module: No Export-ModuleMember -Variable
In manifest:

VariablesToExport = 'anotherVariable'

Result:

In module:

Export-ModuleMember -Variable msgText, anotherVariable

In manifest:

VariablesToExport = '*'

Result:

In module:

Export-ModuleMember -Variable msgText, anotherVariable

In manifest:

VariablesToExport = 'anotherVariable'

Result:

Summary

The export keys in the manifest can be seen as an export override of the Export-ModuleMember used in a module. When testing this behavior with functions alone it can be confusing – at least it confused me – which method to use – manifest export key or Export-ModuleMember – to restrict function exports. When trying to control the exports of variables and aliases it all comes clear: export them in the module at all times!

4 Replies to “PowerShell Export-ModuleMember vs Export keys in manifest

  1. Good article, Anita. From what I can tell, it seems that one benefit to using the manifest file is that you can specify a wildcard and easily export all functions, variables, or aliases. It would be interesting to know if the wildcard can be combined with a string literal, such that you can elect to export all functions, variables, or aliases, that match a pattern (eg. if you’re building a FTP module, then FunctionsToExport = ‘*-Ftp*’).

    Cheers,
    Trevor Sullivan
    http://trevorsullivan.net
    http://twitter.com/pcgeek86

  2. Pingback: PowerShell Export functions, variables and aliases with wildcards

Comments are closed.