🎓 A1Learner
🔍
0 of 47 lessons completed

PowerShell Exporting Members

Exporting members is choosing which functions a module shares with the caller. Only exported functions can be used after the module is imported.

Share one function with Export-ModuleMember

The module file defines a helper and a greeting, but shares only the greeting. The greeting still works because it calls the helper inside the module.

$root = $PSScriptRoot
Set-Content -Path (Join-Path $root "DemoTool.psm1") -Value 'function Get-DemoSuffix { "!" }
function Get-DemoGreeting { param([string]$Name) $Name + (Get-DemoSuffix) }
Export-ModuleMember -Function Get-DemoGreeting'
Import-Module (Join-Path $root "DemoTool.psm1")
Get-Command -Module DemoTool | Select-Object -ExpandProperty Name
Get-DemoGreeting -Name "Ana"

Share functions through the manifest

The module file shares nothing on its own here. The manifest lists the greeting, so only the greeting is shared after import.

$root = $PSScriptRoot
Set-Content -Path (Join-Path $root "DemoTool.psm1") -Value 'function Get-DemoSuffix { "!" }
function Get-DemoGreeting { param([string]$Name) $Name + (Get-DemoSuffix) }'
New-ModuleManifest -Path (Join-Path $root "DemoTool.psd1") -RootModule "DemoTool.psm1" -ModuleVersion "1.0.0" -Author "Ana" -Description "Demo module" -FunctionsToExport @("Get-DemoGreeting")
Import-Module (Join-Path $root "DemoTool.psd1")
Get-Command -Module DemoTool | Select-Object -ExpandProperty Name
Get-DemoGreeting -Name "Bo"

Prove the helper is not shared

The shared list is checked for both names. The helper is missing from it, while the greeting is present and still works.

$root = $PSScriptRoot
Set-Content -Path (Join-Path $root "DemoTool.psm1") -Value 'function Get-DemoSuffix { "!" }
function Get-DemoGreeting { param([string]$Name) $Name + (Get-DemoSuffix) }
Export-ModuleMember -Function Get-DemoGreeting'
Import-Module (Join-Path $root "DemoTool.psm1")
$names = Get-Command -Module DemoTool | Select-Object -ExpandProperty Name
"Helper shared: " + ($names -contains "Get-DemoSuffix")
"Greeting shared: " + ($names -contains "Get-DemoGreeting")
Get-DemoGreeting -Name "Ana"

Example

$root = $PSScriptRoot
Set-Content -Path (Join-Path $root "DemoTool.psm1") -Value 'function Get-DemoSuffix { "!" }
function Get-DemoGreeting { param([string]$Name) $Name + (Get-DemoSuffix) }
Export-ModuleMember -Function Get-DemoGreeting'
New-ModuleManifest -Path (Join-Path $root "DemoTool.psd1") -RootModule "DemoTool.psm1" -ModuleVersion "1.0.0" -Author "Ana" -Description "Demo module" -FunctionsToExport @("Get-DemoGreeting")
Import-Module (Join-Path $root "DemoTool.psd1")
Get-Command -Module DemoTool | Select-Object -ExpandProperty Name
Get-DemoGreeting -Name "Ana"

Key points

  • Exported function: a function a module shares with the caller after import.
  • Use Export-ModuleMember in a module file to choose which functions to share.
  • FunctionsToExport: the manifest setting that lists the functions a module shares.
  • A helper that is not shared stays hidden, even though the module itself can still use it.
  • Use Get-Command -Module to list the functions a module shares.

In this example, the module file and the manifest both name the greeting as shared, and nothing else. After import, the shared list holds only the greeting, and calling it works because its helper runs inside the module.

Note

An exported function can call a hidden helper, and the caller never sees the helper.

Try it Yourself

Edit the code, then press Run.

Running is simulated

Exercise

After importing DemoTool, which command lists the functions the module shares?