New-Module

Create a new dynamic module that exists only in memory.

Syntax
      New-Module [-ScriptBlock] ScriptBlock [-ArgumentList Object[]] [-AsCustomObject]
         [-Cmdlet String[]] [-Function String[]] [-ReturnResult] [CommonParameters]

      New-Module [-Name] String [-ScriptBlock] ScriptBlock [-ArgumentList Object[]]
         [-AsCustomObject] [-Cmdlet String[]] [-Function String[]] [-ReturnResult] [CommonParameters]

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

   -AsCustomObject
       Returns a custom object that represents the dynamic module. The module members are implemented as script 
       methods of the custom object, but they are not imported into the session. 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.

   -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.

   -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.
        
   -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.

   -ReturnResult
       Runs the script block and returns the script block results instead of returning a module object.

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

   CommonParameters:
       -Verbose, -Debug, -ErrorAction, -ErrorVariable, -WarningAction, -WarningVariable,
       -OutBuffer -OutVariable.

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. You can use Export-ModuleMember and the parameters of New-Module to override the defaults.

Dynamic modules are not returned by Get-Module, but the members that they export are returned by Get-Command.

Standard Aliases for New-Module: nmo

Examples

Create a new dynamic module with a function called "Hello". The command returns a module object that represents the new dynamic module:

PS C:\> new-module -scriptblock {function Hello {"Hello!"}}

Create a new dynamic module, and also, use Export-ModuleMember to export a variable into the current session. Without the Export-ModuleMember command, only the function is exported:

PS C:\> New-Module -scriptblock {$SayHelloHelp="Type 'SayHello', a space, and a name.";
                                 function SayHello ($name) { 
                                          "Hello, $name" };
                     Export-ModuleMember -function SayHello -Variable SayHelloHelp }

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

PS C:\> SayHello Jeffrey
 Hello, Jeffrey

for more examples see: get-help New-Module -examples

“If we do not find anything pleasant, at least we shall find something new” ~Voltaire

Related PowerShell Commands:

Get-Module - Get the modules imported to the session
Import-Module - Add a module to the session
Remove-Module - Remove a module from the current session


Copyright © SS64.com 1999-2018
Some rights reserved