CmdID / PowerShell / Cmdlets / New
New-Module
Creates a new dynamic module that exists only in memory. PowerShell reference
From the PowerShell reference
7 parameters
Alias nmo
What it does
Creates a new dynamic module that exists only in memory. The New-Module cmdlet creates a dynamic module from a scriptblock. 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. You can also use the AsCustomObject parameter of New-Module to return the dynamic module as a custom object. The members of the modules, such as functions, are implemented as script methods of the custom object instead of being imported into the session. 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.
New-Module [-ScriptBlock] <ScriptBlock> [-Function <String[]>] [-Cmdlet <String[]>] [-ReturnResult] [-AsCustomObject] [-ArgumentList <Object[]>] [<CommonParameters>]New-Module [-Name] <String> [-ScriptBlock] <ScriptBlock> [-Function <String[]>] [-Cmdlet <String[]>] [-ReturnResult] [-AsCustomObject] [-ArgumentList <Object[]>] [<CommonParameters>]
Create a dynamic module
New-Module -ScriptBlock {function Hello {"Hello!"}}IdentifyWorking with dynamic modules and Get-Module and Get-Command
New-Module -ScriptBlock {function Hello {"Hello!"}}IdentifyExport a variable into the current session
New-Module -ScriptBlock {$SayHelloHelp="Type 'SayHello', a space, and a name."; function SayHello ($Name) { "Hello, $Name" }; Export-ModuleMember -Function SayHello -Variable SayHelloHelp}
$SayHelloHelpIdentifyMake a dynamic module available to Get-Module
New-Module -ScriptBlock {function Hello {"Hello!"}} -Name GreetingModule | Import-Module
Get-ModuleIdentifyGenerate a custom object that has exported functions
$m = New-Module -ScriptBlock {
function Hello ($Name) {"Hello, $Name"}
function Goodbye ($Name) {"Goodbye, $Name"}
} -AsCustomObject
$m
$m | Get-MemberIdentifyGet the results of the scriptblock
New-Module -ScriptBlock {function SayHello {"Hello, World!"}; SayHello} -ReturnResultIdentifyIts page in the reference: New-Module.
Parameters
-ArgumentListSpecifies an array of arguments which are parameter values that are passed to the scriptblock. For more information about the behavior of ArgumentList, see about_Splatting.-AsCustomObjectIndicates that this cmdlet 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 can be accessed from the custom object.-CmdletSpecifies an array of cmdlets that this cmdlet exports 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 scriptblock, but a dynamic module can include cmdlets if it imports the cmdlets from a binary module.-FunctionSpecifies an array of functions that this cmdlet exports 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.-NameSpecifies a name for the new module. You can also pipe a module name to New-Module.
The default value is an autogenerated name that starts with __DynamicModule_ and is followed by a GUID that specifies the path of the dynamic module.-ReturnResultIndicates that this cmdlet runs the scriptblock and returns the scriptblock results instead of returning a module object.-ScriptBlockSpecifies the contents of the dynamic module. Enclose the contents in braces ({}) to create a scriptblock. This parameter is required.