Category Archives: Module

Remove-Module

NAME
    Remove-Module

SYNOPSIS
    Removes modules from the current session.

SYNTAX
    Remove-Module [-ModuleInfo] <PSModuleInfo[]> [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]

    Remove-Module [-Name] <string[]> [-Force] [-Confirm] [-WhatIf] [<CommonParameters>]

DESCRIPTION
    The Remove-Module cmdlet removes the members of a module from the current session.

    If the module includes an assembly (.dll), all members that are implemented by the assembly are removed, but the assembly is not unloaded.

PARAMETERS
    -Force [<SwitchParameter>]
        Removes modules even when their access mode is read-only.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ModuleInfo <PSModuleInfo[]>
        Specifies the module objects to remove. Enter a Variable that contains a module object (PSModuleInfo) or a command that gets a module object, such as a Get-Module command. You can also pipe module objects to Remove-Module.

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

    -Name <string[]>
        Specifies the names of modules to remove. Wildcards are permitted. You can also pipe name strings to Remove-Module.

        Required?                    true
        Position?                    1
        Default value
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? true

    -Confirm [<SwitchParameter>]
        Prompts you for confirmation before executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -WhatIf [<SwitchParameter>]
        Describes what would happen if you executed the command without actually executing the command.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <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, System.Management.Automation.PSModuleInfo
        You can pipe module names (strings) and module objects to Remove-Module.

OUTPUTS
    None
        Remove-Module does not generate any output.

NOTES

        You can also refer to Remove-Module by its Alias, “rmo”. For more information, see about_aliases.

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

    C:\PS>Remove-Module -Name BitsTransfer

    Description
    ———–
    This command removes the BitsTransfer module from the current session.

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

    C:\PS>Get-Module | Remove-Module

    Description
    ———–
    This command removes all modules from the current session.

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

    C:\PS>”FileTransfer”, “PSDiagnostics” | Remove-Module -Verbose

    VERBOSE: Performing operation “Remove-Module” on Target “filetransfer (Path: ‘C:\Windows\system32\WindowsPowerShell\v1.0\Modules\f
    iletransfer\filetransfer.psd1’)”.
    VERBOSE: Performing operation “Remove-Module” on Target “Microsoft.BackgroundIntelligentTransfer.Management (Path: ‘C:\Windows\
    assembly\GAC_MSIL\Microsoft.BackgroundIntelligentTransfer.Management\1.0.0.0__31bf3856ad364e35\Microsoft.BackgroundIntelligentTransfe
    r.Management.dll’)”.
    VERBOSE: Performing operation “Remove-Module” on Target “psdiagnostics (Path: ‘C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
    psdiagnostics\psdiagnostics.psd1’)”.
    VERBOSE: Removing imported Function ‘Start-Trace’.
    VERBOSE: Removing imported Function ‘Stop-Trace’.
    VERBOSE: Removing imported Function ‘Enable-WSManTrace’.
    VERBOSE: Removing imported Function ‘Disable-WSManTrace’.
    VERBOSE: Removing imported Function ‘Enable-PSWSManCombinedTrace’.
    VERBOSE: Removing imported Function ‘Disable-PSWSManCombinedTrace’.
    VERBOSE: Removing imported Function ‘Set-LogProperties’.
    VERBOSE: Removing imported Function ‘Get-LogProperties’.
    VERBOSE: Removing imported Function ‘Enable-PSTrace’.
    VERBOSE: Removing imported Function ‘Disable-PSTrace’.
    VERBOSE: Performing operation “Remove-Module” on Target “PSDiagnostics (Path: ‘C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
    psdiagnostics\PSDiagnostics.psm1’)”.

    Description
    ———–
    This command removes the FileTransfer and PSDiagnostics modules from the current session.

    The command uses a pipeline operator (|) to send the module names to Remove-Module. It uses the Verbose common parameter to get detailed information about the members that are removed.

    The Verbose messages show the items that are removed. The messages differ because the BitsTransfer module includes an assembly that implements its cmdlets and a nested module with its own assembly. The PSDiagnostics module includes a module script file (.psm1) that exports Functions.

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

    C:\PS>$a = Get-Module BitsTransfer

    C:\PS> Remove-Module -moduleInfo $a

    Description
    ———–
    This command uses the ModuleInfo parameter to remove the BitsTransfer module.

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

New-Module

NAME
    New-Module

SYNOPSIS
    Creates a new dynamic module that exists only in memory.

SYNTAX
    New-Module [-Name] <string> [-ScriptBlock] <scriptblock> [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <string[]>] [-Function <string[]>] [-ReturnResult] [<CommonParameters>]

    New-Module [-ScriptBlock] <scriptblock> [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <string[]>] [-Function <string[]>] [-ReturnResult] [<CommonParameters>]

DESCRIPTION
    The New-Module cmdlet creates a dynamic module from a script block. The members of the dynamic module, such as Functions and Variables, are immediately available in the session and remain available until you close the session.

    Like static modules, by default, the cmdlets and Functions in a dynamic module are exported and the Variables and Aliases are not. However, you can use the Export-ModuleMember cmdlet and the parameters of New-Module to override the defaults.

    Dynamic modules exist only in memory, not on disk. Like all modules, the members of dynamic modules run in a private module scope that is a child of the global scope. Get-Module cannot get a dynamic module, but Get-Command can get the exported members.

    To make a dynamic module available to Get-Module, pipe a New-Module command to Import-Module, or pipe the module object that New-Module returns to Import-Module. This action adds the dynamic module to the Get-Module list, but it does not save the module to disk or make it persistent.

PARAMETERS
    -ArgumentList <Object[]>
        Specifies arguments (parameter values) that are passed to the script block.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -AsCustomObject [<SwitchParameter>]
        Returns a custom object with members that represent the module members.

        When you use the AsCustomObject parameter, New-Module creates the dynamic module, imports the module members into the current session, and then returns a PSCustomObject object instead of a PSModuleInfo object. You can save the custom object in a Variable and use dot notation to invoke the members.

        If the module has multiple members with the same name, such as a Function and a Variable that are both named “A,” only one member with each name is accessible from the custom object.

        Required?                    false
        Position?                    named
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Cmdlet <string[]>
        Exports only the specified cmdlets from the module into the current session. Enter a comma-separated list of cmdlets. Wildcard characters are permitted. By default, all cmdlets in the module are exported.

        You cannot define cmdlets in a script block, but a dynamic module can include cmdlets if it imports the cmdlets from a binary module.

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

    -Function <string[]>
        Exports only the specified Functions from the module into the current session. Enter a comma-separated list of Functions. Wildcard characters are permitted. By default, all Functions defined in a module are exported.

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

    -Name <string>
        Specifies a name for the new module. You can also pipe a module name to New-Module.

        The default value is an autogenerated name that begins with “__DynamicModule_” and is followed by a GUID that specifies the path to the dynamic module.

        Required?                    true
        Position?                    1
        Default value                “__DynamicModule_” + GUID
        Accept pipeline input?     true (ByValue)
        Accept wildcard characters? false

    -ReturnResult [<SwitchParameter>]
        Runs the script block and returns the script block results instead of returning a module object.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ScriptBlock <scriptblock>
        Specifies the contents of the dynamic module. Enclose the contents in braces ( { } ) to create a script block. This parameter is required.

        Required?                    true
        Position?                    1
        Default value                None
        Accept pipeline input?     false
        Accept wildcard characters? false

    <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 a module name string to New-Module.

OUTPUTS
    System.Management.Automation.PSModuleInfo, System.Management.Automation.PSCustomObject, or None
        By default, New-Module generates a PSModuleInfo object. If you use the AsCustomObject parameter, it generates a PSCustomObject object. If you use the ReturnResult parameter, it returns the result of evaluating the script block in the dynamic module.

NOTES

        You can also refer to New-Module by its Alias, “nmo”. For more information, see about_aliases.

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

    C:\PS>New-Module -ScriptBlock {function Hello {“Hello!”}}

    Name             : __DynamicModule_2ceb1d0a-990f-45e4-9fe4-89f0f6ead0e5
    Path             : 2ceb1d0a-990f-45e4-9fe4-89f0f6ead0e5
    Description     :
    Guid             : 00000000-0000-0000-0000-000000000000
    Version         : 0.0
    ModuleBase        :
    ModuleType        : Script
    PrivateData     :
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {}
    ExportedFunctions : {[Hello, Hello]}
    ExportedVariables : {}
    NestedModules     : {}

    Description
    ———–
    This command creates a new dynamic module with a Function called “Hello”. The command returns a module object that represents the new dynamic module.

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

    C:\PS>New-Module -ScriptBlock {function Hello {“Hello!”}}

    Name             : __DynamicModule_2ceb1d0a-990f-45e4-9fe4-89f0f6ead0e5
    Path             : 2ceb1d0a-990f-45e4-9fe4-89f0f6ead0e5
    Description     :
    Guid             : 00000000-0000-0000-0000-000000000000
    Version         : 0.0
    ModuleBase        :
    ModuleType        : Script
    PrivateData     :
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {}
    ExportedFunctions : {[Hello, Hello]}
    ExportedVariables : {}
    NestedModules     : {}

    C:\PS> Get-Module
    C:\PS>

    C:\PS> Get-Command Hello

    CommandType     Name Definition
    ———–     —- ———-
    Function        Hello “Hello!”

    Description
    ———–
    This example demonstrates that dynamic modules are not returned by the Get-Module cmdlet, but the members that they export are returned by the Get-Command cmdlet.

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

    C:\PS>New-Module -ScriptBlock {$SayHelloHelp=”Type ‘SayHello’, a space, and a name.”; Function SayHello ($name) { “Hello, $name” }; Export-ModuleMember -Function SayHello -Variable SayHelloHelp}

    C:\PS> $SayHelloHelp
    Type ‘SayHello’, a space, and a name.

    C:\PS> SayHello Jeffrey
    Hello, Jeffrey

    Description
    ———–
    This command uses the Export-ModuleMember cmdlet to export a Variable into the current session. Without the Export-ModuleMember command, only the Function is exported.

    The output shows that both the Variable and the Function were exported into the session.

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

    C:\PS>New-Module -ScriptBlock {function Hello {“Hello!”}} -name GreetingModule | Import-Module

    C:\PS> Get-Module

    Name             : GreetingModule
    Path             : d54dfdac-4531-4db2-9dec-0b4b9c57a1e5
    Description     :
    Guid             : 00000000-0000-0000-0000-000000000000
    Version         : 0.0
    ModuleBase        :
    ModuleType        : Script
    PrivateData     :
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {}
    ExportedFunctions : {[Hello, Hello]}
    ExportedVariables : {}
    NestedModules     : {}

    C:\PS> Get-Command hello

    CommandType     Name                                                             Definition
    ———–     —-                                                             ———-
    Function        Hello                                                             “Hello!”

    Description
    ———–
    This command demonstrates that you can make a dynamic module available to the Get-Module cmdlet by piping the dynamic module to the Import-Module cmdlet.

    The first command uses a pipeline operator (|) to send the module object that New-Module generates to the Import-Module cmdlet. The command uses the Name parameter of New-Module to assign a friendly name to the module. Because Import-Module does not return any objects by default, there is no output from this command.

    The second command uses the Get-Module cmdlet to get the modules in the session. The result shows that Get-Module can get the new dynamic module.

    The third command uses the Get-Command cmdlet to get the Hello Function that the dynamic module exports.

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

    C:\PS>$m = New-Module -ScriptBlock {function Hello ($name) {“Hello, $name”}; Function Goodbye ($name) {“Goodbye, $name”}} -AsCustomObject

    C:\PS> $m

    C:\PS> $m | Get-Member

     TypeName: System.Management.Automation.PSCustomObject

    Name        MemberType Definition
    —-        ———- ———-
    Equals     Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString    Method     string ToString()
    Goodbye     ScriptMethod System.Object Goodbye();
    Hello     ScriptMethod System.Object Hello();

    PS C:\ps-test> $m.goodbye(“Jane”)
    Goodbye, Jane

    PS C:\ps-test> $m.hello(“Manoj”)
    Hello, Manoj

    PS C:\ps-test> goodbye Jane
    Goodbye, Jane

    PS C:\ps-test> hello Manoj
    Hello, Manoj

    Description
    ———–
    This example shows how to use the AsCustomObject parameter of New-Module to generate a custom object with script methods that represent the exported Functions.

    The first command uses the New-Module cmdlet to generate a dynamic module with two Functions, Hello and Goodbye. The command uses the AsCustomObject parameter to generate a custom object instead of the PSModuleInfo object that New-Module generates by default. The command saves the custom object in the $m Variable.

    The second command attempts to display the value of the $m Variable. No content appears.

    The third command uses a pipeline operator (|) to send the custom object to the Get-Member cmdlet, which displays the properties and methods of the custom object. The output shows that the object has script methods that represent the Hello and Goodbye Functions.

    The fourth and fifth commands use the script method format to call the Hello and Goodbye Functions.

    The sixth and seventh commands call the Functions by specifying the Function name and parameter value.

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

    C:\PS>New-Module -ScriptBlock {function SayHello {“Hello, World!”}; SayHello} -ReturnResult

    Hello, World!

    Description
    ———–
    This command uses the ReturnResult parameter to request the results of running the script block instead of requesting a module object.

    The script block in the new module defines the SayHello Function and then calls the Function.

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

Import-Module

NAME
    Import-Module

SYNOPSIS
    Adds modules to the current session.

SYNTAX
    Import-Module [-Name] <string[]> [-Alias <string[]>] [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <string[]>] [-Force] [-Function <string[]>] [-Global] [-PassThru] [-Prefix <string>] [-Variable <string[]>] [-Version <Version>] [<CommonParameters>]

    Import-Module [-Assembly] <Assembly[]> [-Alias <string[]>] [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <string[]>] [-Force] [-Function <string[]>] [-Global] [-PassThru] [-Prefix <string>] [-Variable <string[]>] [-Version <Version>] [<CommonParameters>]

    Import-Module [-ModuleInfo] <PSModuleInfo[]> [-Alias <string[]>] [-ArgumentList <Object[]>] [-AsCustomObject] [-Cmdlet <string[]>] [-Force] [-Function <string[]>] [-Global] [-PassThru] [-Prefix <string>] [-Variable <string[]>] [-Version <Version>] [<CommonParameters>]

DESCRIPTION
    The Import-Module cmdlet adds one or more modules to the current session.

    A module is a package that contains members (such as cmdlets, providers, scripts, Functions, Variables, and other tools and files) that can be used in Windows PowerShell. After a module is imported, you can use the module members in your session.

    To import a module, use the Name, Assembly, or ModuleInfo parameter to identify the module to import. By default, Import-Module imports all members that the module exports, but you can use the Alias, Function, Cmdlet, and Variable parameters to restrict the members that are imported.

    Import-Module imports a module only into the current session. To import the module into all sessions, add an Import-Module command to your Windows PowerShell profile. For more information about profiles, see about_profiles.

    For more information about modules, see about_modules.

PARAMETERS
    -Alias <string[]>
        Imports only the specified Aliases from the module into the current session. Enter a comma-separated list of Aliases. Wildcard characters are permitted.

        Some modules automatically export selected Aliases into your session when you import the module. This parameter lets you select from among the exported Aliases.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -ArgumentList <Object[]>
        Specifies arguments (parameter values) that are passed to a script module during the Import-Module command. This parameter is valid only when you are importing a script module.

        You can also refer to ArgumentList by its Alias, “args”. For more information, see about_aliases.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -AsCustomObject [<SwitchParameter>]
        Returns a custom object with members that represent the imported module members. This parameter is valid only for script modules.

        When you use the AsCustomObject parameter, Import-Module imports the module members into the session and then returns a PSCustomObject object instead of a PSModuleInfo object. You can save the custom object in a Variable and use dot notation to invoke the members.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Assembly <Assembly[]>
        Imports the cmdlets and providers implemented in the specified assembly objects. Enter a Variable that contains assembly objects or a command that creates assembly objects. You can also pipe an assembly object to Import-Module.

        When you use this parameter, only the cmdlets and providers implemented by the specified assemblies are imported. If the module contains other files, they are not imported, and you might be missing important members of the module. Use this parameter for debugging and testing the module, or when you are instructed to use it by the module author.

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

    -Cmdlet <string[]>
        Imports only the specified cmdlets from the module into the current session. Enter a list of cmdlets. Wildcard characters are permitted.

        Some modules automatically export selected cmdlets into your session when you import the module. This parameter lets you select from among the exported cmdlets.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Force [<SwitchParameter>]
        Re-imports a module and its members, even if the module or its members have an access mode of read-only.

        Required?                    false
        Position?                    named
        Default value                False
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Function <string[]>
        Imports only the specified Functions from the module into the current session. Enter a list of Functions. Wildcard characters are permitted.

        Some modules automatically export selected Functions into your session when you import the module. This parameter lets you select from among the exported Functions.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Global [<SwitchParameter>]
        When used in a script module (.psm1), this parameter imports modules into the global session state.

        This parameter is effective only when it appears in a script module. Otherwise, it is ignored.

        By default, the commands in a script module, including commands from nested modules, are imported into the caller’s session state. To restrict the commands that a module exports, use an Export-ModuleMember command in the script module.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ModuleInfo <PSModuleInfo[]>
        Specifies module objects to import. Enter a Variable that contains the module objects, or a command that gets the module objects, such as a “Get-Module -listavailable” command. You can also pipe module objects to Import-Module.

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

    -Name <string[]>
        Specifies the names of the modules to import. Enter the name of the module or the name of a file in the module, such as a .psd1, .psm1, .dll, or ps1 file. File paths are optional. Wildcards are not permitted. You can also pipe module names and file names to Import-Module.

        If you omit a path, Import-Module looks for the module in the paths saved in the PSModulePath Environment Variable ($env:PSModulePath).

        Specify only the module name whenever possible. When you specify a file name, only the members that are implemented in that file are imported. If the module contains other files, they are not imported, and you might be missing important members of the module.

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

    -PassThru [<SwitchParameter>]
        Returns objects that represent the modules that were imported. By default, this cmdlet does not generate any output.

        Notes
        — When you pipe the output of a “Get-Module -listavailable” command to an Import-Module command with the PassThru parameter, Import-Module returns the object that Get-Module passed to it without updating the object. As a result, the Exported and NestedModules properties are not yet populated.

        — When you use the Prefix parameter to specify a prefix for the member, the prefix does not appear in the member names in the properties of the module object. The object records what was exported before the prefix was applied.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Prefix <string>
        Adds the specified prefix to the nouns in the names of imported module members.

        Use this parameter to avoid name conflicts that might occur when different members in the session have the same name. This parameter does not change the module, and it does not affect files that the module imports for its own use (known as “nested modules”). It affects only the names of members in the current session.

        For example, if you specify the prefix “UTC” and then import a Get-Date cmdlet, the cmdlet is known in the session as Get-UTCDate, and it is not confused with the original Get-Date cmdlet.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Variable <string[]>
        Imports only the specified Variables from the module into the current session. Enter a list of Variables. Wildcard characters are permitted.

        Some modules automatically export selected Variables into your session when you import the module. This parameter lets you select from among the exported Variables.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? true

    -Version <Version>
        Specifies the version of the module to import. Use this parameter when you have different versions of the same module on your system.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    <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, System.Management.Automation.PSModuleInfo, System.Reflection.Assembly
        You can pipe a module name, module object, or assembly object to Import-Module.

OUTPUTS
    None, System.Management.Automation.PSModuleInfo, or System.Management.Automation.PSCustomObject
        By default, Import-Module does not generate any output. If you use the PassThru parameter, it generates a System.Management.Automation.PSModuleInfo object that represents the module. If you use the AsCustomObject parameter, it generates a PSCustomObject object.

NOTES

        You can also refer to Import-Module by its Alias, “ipmo”. For more information, see about_aliases.

        Before you can import a module, the module directory must be copied to a directory that is accessible to your local computer. For more information, see about_modules.

        Module members run in their own private module session state, so the commands that they use for internal processing do not affect your session state.

        If you import members with the same name and the same type into your session, Windows PowerShell uses the member imported last by default. Variables and Aliases are replaced, and the originals are not accessible. Functions, cmdlets and providers are merely “shadowed” by the new members, and they can be accessed by qualifying the command name with the name of its snap-in, module, or Function path.

        To update the formatting data for commands that have been imported from a module, use the Update-FormatData cmdlet. Update-FormatData also updates the formatting data for commands in the session that were imported from modules. If the formatting file for a module changes, you can run an Update-FormatData command to update the formatting data for imported commands. You do not need to import the module again.

        To import a module that is created by Import-PSSession or Export-PSSession, the execution policy in the current session cannot be Restricted or AllSigned, because the modules that Import-PSSession and Export-PSSession create contains unsigned script files that are prohibited by these policies. To use Import-Module without changing the execution policy for the local computer, use the Scope parameter of Set-ExecutionPolicy to set a less restrictive execution policy for a single process.

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

    C:\PS>Import-Module -Name BitsTransfer

    Description
    ———–
    This command imports the members of the BitsTransfer module into the current session.

    The Name parameter name (-Name) is optional and can be omitted.

    By default, Import-Module does not generate any output when it imports a module. To request output, use the PassThru or AsCustomObject parameter, or the Verbose common parameter.

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

    C:\PS>Get-Module -listAvailable | Import-Module

    Description
    ———–
    This command imports all available modules in the path specified by the PSModulePath Environment Variable ($env:psmodulepath) into the current session.

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

    C:\PS>$m = Get-Module -ListAvailable BitsTransfer, ServerBackup

    C:\PS> Import-Module -moduleInfo $m

    Description
    ———–
    These commands import the members of the BitsTransfer and ServerBackup modules into the current session.

    The first command uses the Get-Module cmdlet to get PSModuleInfo objects that represent the BitsTransfer and ServerBackup modules. It saves the objects in the $m Variable. The ListAvailable parameter is required when you are getting modules that are not yet imported into the session.

    The second command uses the ModuleInfo parameter of Import-Module to import the modules into the current session.

    These commands are equivalent to using a pipeline operator (|) to send the output of a Get-Module command to Import-Module.

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

    C:\PS>Import-Module -Name c:\ps-test\modules\test -Verbose

    VERBOSE: Loading module from path ‘C:\ps-test\modules\Test\Test.psm1’.
    VERBOSE: Exporting Function ‘my-parm’.
    VERBOSE: Exporting Function ‘get-parm’.
    VERBOSE: Exporting Function ‘get-spec’.
    VERBOSE: Exporting Function ‘get-specDetails’.

    Description
    ———–
    This command uses an explicit path to identify the module to import.

    It also uses the Verbose common parameter to get a list of the items imported from the module. Without the Verbose, PassThru, or AsCustomObject parameter, Import-Module does not generate any output when it imports a module.

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

    C:\PS>Import-Module BitsTransfer -Cmdlet Add-BitsTransferFile, Get-BitsTransfer

    C:\PS> Get-Module BitsTransfer

    Name             : BitsTransfer
    Path             : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer\BitsTransfer.psd1
    Description     :
    Guid             : 8fa5064b-8479-4c5c-86ea-0d311fe48875
    Version         : 1.0.0.0
    ModuleBase        : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\BitsTransfer
    ModuleType        : Manifest
    PrivateData     :
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {[Add-BitsTransfer, Add-BitsTransfer], [Complete-BitsTransfer, Complete-BitsTransfer], [Get-BitsTransfer, Get-BitsTransfer], [Rem
                        ove-BitsTransfer, Remove-BitsTransfer]…}
    ExportedFunctions : {}
    ExportedVariables : {}
    NestedModules     : {Microsoft.BackgroundIntelligentTransfer.Management}

    C:\PS> Get-Command -module BitsTransfer

    CommandType Name                Definition
    ———– —-                ———-
    Cmdlet     Add-BitsTransfer    Add-BitsTransfer [-BitsJob] <BitsJob[]> [-Source] <String[]> [[-Destination] <String[]>] [-Verbose] [-Debug] [-ErrorA…
    Cmdlet     Get-BitsTransfer    Get-BitsTransfer [[-Name] <String[]>] [-AllUsers] [-Verbose] [-Debug] [-ErrorAction <ActionPreference>] [-WarningActi…

    Description
    ———–
    This example shows how to restrict the module members that are imported into the session and the effect of this command on the session.

    The first command imports only the Add-BitsTransfer and Get-BitsTransfer cmdlets from the BitsTransfer module. The command uses the Cmdlet parameter to restrict the cmdlets that the module imports. You can also use the Alias, Variable, and Function parameters to restrict other members that a module imports.

    The second command uses the Get-Module cmdlet to get the object that represents the BitsTransfer module. The ExportedCmdlets property lists all of the cmdlets that the module exports, even when they were not all imported.

    The third command uses the Module parameter of the Get-Command cmdlet to get the commands that were imported from the BitsTransfer module. The results confirm that only the Add-BitsTransfer and Get-BitsTransfer cmdlets were imported.

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

    C:\PS>Import-Module BitsTransfer -Prefix PS -PassThru

    Name             : bitstransfer
    Path             : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\bitstransfer\bitstransfer.psd1
    Description     :
    Guid             : 8fa5064b-8479-4c5c-86ea-0d311fe48875
    Version         : 1.0.0.0
    ModuleBase        : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\bitstransfer
    ModuleType        : Manifest
    PrivateData     :
    AccessMode        : ReadWrite
    ExportedAliases : {}
    ExportedCmdlets : {[Add-BitsTransfer, Add-BitsTransfer], [Remove-BitsTransfer, Remove-BitsTransfer], [Complete-BitsTransfer, Complete-BitsTransfer]
                        , [Get-BitsTransfer, Get-BitsTransfer]…}
    ExportedFunctions : {}
    ExportedVariables : {}
    NestedModules     : {Microsoft.BackgroundIntelligentTransfer.Management}

    C:\PS> Get-Command -module bitstransfer

    CommandType     Name                        Definition
    ———–     —-                        ———-
    Cmdlet         Add-PSBitsTransfer         Add-PSBitsTransfer [-BitsJob] <BitsJob[]> [-Source] <String[]> …
    Cmdlet         Complete-PSBitsTransfer     Complete-PSBitsTransfer [-BitsJob] <BitsJob[]> [-Verbose] [-Deb…
    Cmdlet         Get-PSBitsTransfer         Get-PSBitsTransfer [[-Name] <String[]>] [-AllUsers] [-Verbose] …
    Cmdlet         Remove-PSBitsTransfer     Remove-PSBitsTransfer [-BitsJob] <BitsJob[]> [-Verbose] [-Debug
    Cmdlet         Resume-PSBitsTransfer     Resume-PSBitsTransfer [-BitsJob] <BitsJob[]> [-Asynchronous] [-…
    Cmdlet         Set-PSBitsTransfer         Set-PSBitsTransfer [-BitsJob] <BitsJob[]> [-DisplayName <String…
    Cmdlet         Start-PSBitsTransfer        Start-PSBitsTransfer [[-Source] <String[]>] [[-Destination] <St…
    Cmdlet         Suspend-PSBitsTransfer     Suspend-PSBitsTransfer [-BitsJob] <BitsJob[]> [-Verbose] [-Debu…

    Description
    ———–
    These commands import the BitsTransfer module into the current session, add a prefix to the member names, and then display the prefixed member names.

    The first command uses the Import-Module cmdlet to import the BitsTransfer module. It uses the Prefix parameter to add the PS prefix to all members that are imported from the module and the PassThru parameter to return a module object that represents the imported module.

    The module object that the command returns has an ExportedCmdlets property that lists the exported members. The prefix does not appear in the cmdlet names, because it is applied after the members are exported (but before they are imported).

    The second command uses the Get-Command cmdlet to get the members that have been imported from the module. It uses the Module parameter to specify the module. The output shows that the module members were correctly prefixed.

    The prefix that you use applies only to the members in the current session. It does not change the module.

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

    C:\PS>Get-Module -list | Format-Table -property name, moduletype -auto

    Name         ModuleType
    —-         ———-
    Show-Calendar     Script
    BitsTransfer    Manifest
    PSDiagnostics Manifest
    TestCmdlets     Script

    C:\PS> $a = Import-Module -Name Show-Calendar -AsCustomObject

    C:\PS> $a | Get-Member

     TypeName: System.Management.Automation.PSCustomObject

    Name         MemberType Definition
    —-         ———- ———-
    Equals        Method     bool Equals(System.Object obj)
    GetHashCode Method     int GetHashCode()
    GetType     Method     type GetType()
    ToString     Method     string ToString()
    Show-Calendar ScriptMethod System.Object Show-Calendar();

    C:\PS> $a.”show-calendar”()

    Description
    ———–
    These commands demonstrate how to get and use the custom object that Import-Module returns.

    Custom objects include synthetic members that represent each of the imported module members. For example, the cmdlets and Functions in a module are converted to script methods of the custom object.

    Custom objects are very useful in scripting. They are also useful when several imported objects have the same names. Using the script method of an object is equivalent to specifying the fully qualified name of an imported member, including its module name.

    The AsCustomObject parameter can be used only with a script module, so the first task is to determine which of the available modules is a script module.

    The first command uses the Get-Module cmdlet to get the available modules. The command uses a pipeline operator (|) to pass the module objects to the Format-Table cmdlet, which lists the Name and ModuleType of each module in a table.

    The second command uses the Import-Module cmdlet to import the Show-Calendar script module. The command uses the AsCustomObject parameter to request a custom object. The command saves the resulting custom object in the $a Variable.

    The third command uses a pipeline operator to send the $a Variable to the Get-Member cmdlet, which gets the properties and methods of the PSCustomObject in $a. The output shows a Show-Calendar script method.

    The last command uses the Show-Calendar script method. The method name must be enclosed in quotation marks, because it includes a hyphen.

    ————————– EXAMPLE 8 ————————–

    C:\PS>Import-Module BitsTransfer

    C:\PS> Import-Module BitsTransfer -Force -Prefix PS

    Description
    ———–
    This example shows how to use the Force parameter of Import-Module when you are re-importing a module into the same session.

    The first command imports the BitsTransfer module. The second command imports the module again, this time using the Prefix parameter.

    The second command also includes the Force parameter, which removes the module and then imports it again. Without this parameter, the session would include two copies of each BitsTransfer cmdlet, one with the standard name and one with the prefixed name.

    ————————– EXAMPLE 9 ————————–

    C:\PS>Get-Date

    Saturday, September 12, 2009 6:47:04 PM

    C:\PS> Import-Module TestModule

    C:\PS> Get-Date
    09255

    C:\PS> Get-Command Get-Date | Format-Table -property commandtype, name, pssnapin, module -auto

    CommandType Name     pssnapin                     Module
    ———– —-     ——–                     ——
     Function Get-Date                                 TestModule
         Cmdlet Get-Date Microsoft.PowerShell.Utility

    C:\PS> Microsoft.PowerShell.Utility\Get-Date

    Saturday, September 12, 2009 6:33:23 PM

    Description
    ———–
    This example shows how to run commands that have been hidden by imported commands.

    The first command run the Get-Date cmdlet that comes with Windows PowerShell. It returns a DateTime object with the current date.

    The second command imports the TestModule module. This module includes a Function named Get-Date that returns the Julian date.

    The third command runs the Get-Date command again. Because Functions take precedence over cmdlets, the Get-Date Function from the TestModule module ran, instead of the Get-Date cmdlet.

    The fourth command shows that there are two Get-Date commands in the session, a Function from the TestModule module and a cmdlet from the Microsoft.PowerShell.Utility snap-in.

    The fifth command runs the hidden cmdlet by qualifying the command name with the snap-in name.

    For more information about command precedence in Windows PowerShell, see about_command_precedence.

RELATED LINKS
    Online version: http://go.microsoft.com/fwlink/?LinkID=141553
    Get-Module
    New-Module
    Remove-Module
    Export-ModuleMember
    about_modules

Get-Module

NAME
    Get-Module

SYNOPSIS
    Gets the modules that have been imported or that can be imported into the current session.

SYNTAX
    Get-Module [-All] [-ListAvailable] [-Name <string[]>] [<CommonParameters>]

    Get-Module [[-Name] <string[]>] [<CommonParameters>]

DESCRIPTION
    The Get-Module cmdlet gets the modules that have been imported, or that can be imported, into the session.

    Get-Module only gets modules; it does not import them. To import the modules into your session, use Import-Module.

PARAMETERS
    -All [<SwitchParameter>]
        Gets module objects for all module files.

        Without the All parameter, Get-Module gets only the module object for the default module file. The cmdlet selects file types in the following order: manifest (.psd1) files, script module (.psm1) files, and binary module (.dll) files.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -ListAvailable [<SwitchParameter>]
        Gets all of the modules that can be imported into the session. Get-Module gets the modules in the paths specified by the $env:PSModulePath Environment Variable.

        Without this parameter, Get-Module gets only the modules that have been imported into the session.

        Required?                    false
        Position?                    named
        Default value
        Accept pipeline input?     false
        Accept wildcard characters? false

    -Name <string[]>
        Gets only modules with the specified names or name patterns. Wildcards are permitted. You can also pipe the names to Get-Module.

        Required?                    false
        Position?                    1
        Default value                All imported or available modules.
        Accept pipeline input?     true (ByValue)
        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 module names to Get-Module.

OUTPUTS
    System.Management.Automation.PSModuleInfo
        Get-Module returns objects that represent the modules.

NOTES

        You can also refer to Get-Module by its Alias, “gmo”. For more information, see about_aliases.

        The All parameter returns module objects for all files with a .dll file name extension, even if they do not implement cmdlets or providers.

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

    C:\PS>Get-Module

    Description
    ———–
    This command gets the modules that have been imported into the current session.

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

    C:\PS>Get-Module -listAvailable

    Description
    ———–
    This command gets the modules that can be imported into the current session.

    Get-Module looks for available modules in the path specified by the $env:PSModulePath Environment Variable. For more information about PSModulePath, see about_modules and about_environment_variables.

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

    C:\PS>Get-Module -listAvailable -All

    Description
    ———–
    This command gets all of the exported files for all available modules.

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

    C:\PS>Get-Module | Get-Member -type property

     TypeName: System.Management.Automation.PSModuleInfo

    Name             MemberType Definition
    —-             ———- ———-
    AccessMode        Property System.Management.Automation.ModuleAcc
    Description     Property System.String Description {get;set;}
    ExportedAliases Property System.Collections.Generic.Dictionary`
    ExportedCmdlets Property System.Collections.Generic.Dictionary`
    ExportedFunctions Property System.Collections.Generic.Dictionary`
    ExportedVariables Property System.Collections.Generic.Dictionary`
    Guid             Property System.Guid Guid {get;}
    ModuleBase        Property System.String ModuleBase {get;}
    ModuleType        Property System.Management.Automation.ModuleTyp
    Name             Property System.String Name {get;}
    NestedModules     Property System.Collections.ObjectModel.ReadOnl
    OnRemove         Property System.Management.Automation.ScriptBlo
    Path             Property System.String Path {get;}
    PrivateData     Property System.Object PrivateData {get;set;}
    SessionState     Property System.Management.Automation.SessionSt
    Version         Property System.Version Version {get;}

    Description
    ———–
    This command get the properties of the PSModuleInfo object that Get-Module returns. There is one object for each module file.

    You can use the properties to format and filter the module objects. For more information about the properties, see “PSModule Properties” in the MSDN (Microsoft Developer Network) library at http://go.microsoft.com/fwlink/?LinkId=143624.

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

    C:\PS>Get-Module -listAvailable -All | Format-Table -property name, moduletype, path -groupby name -auto

     Name: MyTestCmdlets

    Name         ModuleType Path
    —-         ———- —-
    MyTestCmdlets     Script C:\Windows\system32\WindowsPowerShell\v1.0\Modules\TestCmdlets\TestCmdlets.dll

     Name: PSDiagnostics

    Name         ModuleType Path
    —-         ———- —-
    PSDiagnostics Manifest C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psd1
    PSDiagnostics     Script C:\Windows\system32\WindowsPowerShell\v1.0\Modules\PSDiagnostics\PSDiagnostics.psm1

     Name: FileTransfer

    Name         ModuleType Path
    —-         ———- —-
    FileTransfer Manifest C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer\FileTransfer.psd1

    Description
    ———–
    This command gets all module files (imported and available) and groups them by module name. This lets you see the module files that each script is exporting.

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

    C:\PS>$m = Get-Module -list -Name FileTransfer | where {$_.moduletype -eq “Manifest”}

    C:\PS> Get-Content $m.path

    @{
    GUID=”{8FA5064B-8479-4c5c-86EA-0D311FE48875}”
    Author=”Microsoft Corporation”
    CompanyName=”Microsoft Corporation”
    Copyright=”© Microsoft Corporation. All rights reserved.”
    ModuleVersion=”1.0.0.0″
    Description=”Windows Powershell File Transfer Module”
    PowerShellVersion=”2.0″
    CLRVersion=”2.0″
    NestedModules=”Microsoft.BackgroundIntelligentTransfer.Management”
    FormatsToProcess=”FileTransfer.Format.ps1xml”
    RequiredAssemblies=Join-Path $psScriptRoot “Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll”
    }

    Description
    ———–
    These commands display the contents of the module manifest for the Windows PowerShell File Transfer module.

    The first command gets the PSModuleInfo object that represent the module manifest for the File Transfer module. It saves the object in the $m Variable.

    The second command uses dot notation to get the path to the manifest file, which is stored in the Path property of the object. Then, it uses the Get-Content cmdlet to get the content of the manifest file in the specified path.

    Modules are not required to have manifest files. When they do have a manifest file, a manifest is required only to include a version number. However, manifest files often provide useful information about a module, its requirements, and its contents.

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

    C:\PS>Get-Module -listAvailable -Name FileTransfer | Format-List -property *

    Name             : FileTransfer
    Path             : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer\FileTransfer.psd1
    Description     : Powershell File Transfer Module
    Guid             : 8fa5064b-8479-4c5c-86ea-0d311fe48875
    ModuleBase        : C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer
    PrivateData     :
    Version         : 1.0.0.0
    ModuleType        : Manifest
    AccessMode        : ReadWrite
    ExportedFunctions : {}
    ExportedCmdlets : {}
    NestedModules     : {}
    ExportedVariables : {}
    ExportedAliases : {}
    SessionState     : System.Management.Automation.SessionState
    OnRemove         :

    Description
    ———–
    This command displays all of the properties of the FileTransfer module in a list.

    Because the module has not yet been imported into the session, the Exported* properties and the NestedModules property are not yet populated. These properties are populated only after the elements have been exported and the nested modules have been instantiated.

    ————————– EXAMPLE 8 ————————–

    C:\PS>dir (Get-Module -listavailable FileTransfer).modulebase

        Directory: C:\Windows\system32\WindowsPowerShell\v1.0\Modules\FileTransfer

    Mode                LastWriteTime     Length Name
    —-                ————-     —— —-
    d—-        12/16/2008 12:36 PM            en-US
    -a—        11/19/2008 11:30 PM     16184 FileTransfer.Format.ps1xml
    -a—        11/20/2008 11:30 PM     1044 FileTransfer.psd1
    -a—        12/16/2008 12:20 AM     108544 Microsoft.BackgroundIntelligentTransfer.Management.Interop.dll

    Description
    ———–
    This command lists the files in the module’s directory. This is another way to determine what is in a module before you import it. Some modules might have help files or ReadMe files that describe the module.

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