Category Archives: ModuleMember

Export-ModuleMember

NAME
    Export-ModuleMember

SYNOPSIS
    Specifies the module members that are exported.

SYNTAX
    Export-ModuleMember [[-Function] <string[]>] [-Alias <string[]>] [-Cmdlet <string[]>] [-Variable <string[]>] [<CommonParameters>]

DESCRIPTION
    The Export-ModuleMember cmdlet specifies the module members (such as cmdlets, Functions, Variables, and Aliases) that are exported from a script module (.psm1) file, or from a dynamic module created by using the New-Module cmdlet. This cmdlet can be used only in a script module file or a dynamic module.

    If a script module does not include an Export-ModuleMember command, the Functions in the script module are exported, but the Variables and Aliases are not. When a script module includes an Export-ModuleMember command, only the members specified in the Export-ModuleMember command are exported.

    If a script module contains multiple Export-ModuleMember commands, only the members listed in an Export-ModuleMember command are exported.

    You can also use Export-ModuleMember to export members that the script module imports from other modules.

PARAMETERS
    -Alias <string[]>
        Specifies the Aliases that are exported from the script module file. Enter the Alias names. Wildcards are permitted.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Cmdlet <string[]>
        Specifies the cmdlets that are exported from the script module file. Enter the cmdlet names. Wildcards are permitted.

        You cannot create cmdlets in a script module file, but you can import cmdlets from a binary module into a script module and re-export them from the script module.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    -Function <string[]>
        Specifies the Functions that are exported from the script module file. Enter the Function names. Wildcards are permitted. You can also pipe Function name strings to Export-ModuleMember.

        Required?                    false
        Position?                    1
        Default value                None
        Accept pipeline input?     true (ByValue, ByPropertyName)
        Accept wildcard characters? true

    -Variable <string[]>
        Specifies the Variables that are exported from the script module file. Enter the Variable names (without a dollar sign). Wildcards are permitted.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     true (ByPropertyName)
        Accept wildcard characters? true

    <CommonParameters>
        This cmdlet supports the common parameters: Verbose, Debug,
        ErrorAction, ErrorVariable, WarningAction, WarningVariable,
        OutBuffer and OutVariable. For more information, type,
        “Get-Help about_CommonParameters“.

INPUTS
    System.String
        You can pipe Function name strings to Export-ModuleMember.

OUTPUTS
    None
        This cmdlet does not generate any output.

NOTES

        To exclude a member from the list of exported members, add an Export-ModuleMember command that lists all other members but omits the member that you want to exclude.

    ————————– EXAMPLE 1 ————————–

    C:\PS>Export-ModuleMember -Function * -Alias *

    Description
    ———–
    This command exports the Aliases defined in the script module, along with the Functions defined in the script module.

    To export the Aliases, which are not exported by default, you must also explicitly specify the Functions. Otherwise, only the Aliases will be exported.

    ————————– EXAMPLE 2 ————————–

    C:\PS>Export-ModuleMember -Function Get-Test, New-Test, Start-Test -Alias gtt, ntt, stt

    Description
    ———–
    This command exports three Aliases and three Functions defined in the script module.

    You can use this command format to specify the names of module members.

    ————————– EXAMPLE 3 ————————–

    C:\PS>Export-ModuleMember

    Description
    ———–
    This command specifies that no members defined in the script module are exported.

    This command prevents the module members from being exported, but it does not hide the members. Users can read and copy module members or use the call operator (&) to invoke module members that are not exported.

    ————————– EXAMPLE 4 ————————–

    C:\PS>Export-ModuleMember -Variable increment

    Description
    ———–
    This command exports only the $increment Variable from the script module. No other members are exported.

    If you want to export a Variable, in addition to exporting the Functions in a module, the Export-ModuleMember command must include the names of all of the Functions and the name of the Variable.

    ————————– EXAMPLE 5 ————————–

    C:\PS># From TestModule.psm1

    Function new-test
        { <function code> }
    Export-ModuleMember -Function new-test

    Function validate-test
        { <function code> }

    Function start-test
        { <function code> }
    Set-Alias stt start-test
    Export-ModuleMember -Function *-test -Alias stt

    Description
    ———–
    These commands show how multiple Export-ModuleMember commands are interpreted in a script module (.psm1) file.

    These commands create three Functions and one Alias, and then they export two of the Functions and the Alias.

    Without the Export-ModuleMember commands, all three of the Functions would be exported, but the Alias would not be exported. With the Export-ModuleMember commands, only the Get-Test and Start-Test Functions and the STT Alias are exported.

    ————————– EXAMPLE 6 ————————–

    C:\PS>New-Module -script {function SayHello {“Hello!”}; Set-Alias Hi SayHello; Export-ModuleMember -Alias Hi -Function SayHello}

    Description
    ———–
    This command shows how to use Export-ModuleMember in a dynamic module that is created by using the New-Module cmdlet.

    In this example, Export-ModuleMember is used to export both the “Hi” Alias and the “SayHello” Function in the dynamic module.

    ————————– EXAMPLE 7 ————————–

    C:\PS>function export
    {
        param (
            [parameter(mandatory=$true)] [validateset(“function”,”variable”)] $type,
            [parameter(mandatory=$true)] $name,
            [parameter(mandatory=$true)] $value
        )
        if ($type -eq “function”)
        {
            Set-Item “function:script:$name” $value
            Export-ModuleMember $name
        }
        else
        {
            Set-Variable -scope Script $name $value
            Export-ModuleMember -Variable $name
        }
    }

    export Function New-Test
    {
     …
    }

    Function helper
    {
     …
    }

    export Variable interval 0
    $interval = 2

    Description
    ———–
    This example includes a Function named Export that declares a Function or creates a Variable, and then writes an Export-ModuleMember command for the Function or Variable. This lets you declare and export a Function or Variable in a single command.

    To use the Export Function, include it in your script module. To export a Function, type “Export” before the Function keyword.

    To export a Variable, use the following format to declare the Variable and set its value:

        export Variable <variable-name> <value>

    The commands in the example show the correct format. In this example, only the New-Test Function and the $Interval Variable are exported.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=141551
    Import-Module
    Get-Module
    Remove-Module
    about_modules