PowerShell Export functions, variables and aliases with wildcards

23 Apr

In my previous post ‘PowerShell Export-ModuleMember vs Export keys in manifest‘ I wrote about ways to export various items from a module.

Trevor Sullivan read my post and suggested in his comment to analyze the use of wildcards to export functions, variables or aliases using the manifest file.

After trying this I got enthusiastic about it, read further…

As well in the manifest file as with the use of Export-ModuleMember in the module wildcards can be used.

This can be (very) helpful when naming the function in the module according to a certain structure.
In C# accessibility levels are used to restrict or limit access to certain methods. There is no such thing in PowerShell, but this can be achieved by (not) exporting the functions from a module. In C# reserved accessibility levels as public and private are used. We can use this ‘technique’ in PowerShell by naming the functions in a module according to a standard: use for example -public or _public at the end of a functionname or variable in a module and use the same string literal as wildcard to export functions.

Practically this means defining Export-ModuleMember in the module like:
Export-ModuleMember -Function “*”
Export-ModuleMember -Variable “*”
Export-ModuleMember -Alias “*”
to export all functions, variables and aliases.
Then restrict the exported items in the module manifest like:
FunctionsToExport = ‘*-Public’
VariablesToExport = ‘*_Public’
AliasesToExport = ‘*-Public’

Module (psm1)

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

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

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

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

Manifest (psd1)

# Functions to export from this module
FunctionsToExport = '*-Public'

# Variables to export from this module
VariablesToExport = '*_Public'

# Aliases to export from this module
AliasesToExport = '*-Public'

And the result:

Summary

In my previous post I wrote in the summary:
‘The export keys in the manifest can be seen as an export override of the Export-ModuleMember used in a module… When trying to control the exports of variables and aliases it all comes clear: export them in the module at all times!’

After analyzing the wildcard options to export I still agree on the above statements, but I would like to nuance it a bit: export all items in the module and restrict the exported items in the module manifest by using wildcards in the module manifest and using a naming convention when defining functions, variables and aliases.

When approaching exporting the items from a module as described above you can not forget to export a function, variable or alias, you just have to follow this naming convention.