Tag Archives: ReturnResult

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