Share this post:This blog post is part of an ongoing series by Adam Gordon. Adam will walk through each PowerShell command every week, explaining when and how to use them. This week Adam will be covering New-Module.
When should you use a new-module?
The New-Module cmdlet creates dynamic modules from a script block. The dynamic module members, such as variables and functions, are immediately available and available until you close your session.
As static modules, the cmdlets, functions, and variables in dynamic modules are exported by default. Variables and aliases, however, are not. To override defaults, you can use Export-ModuleMember and the parameters from New-Module.
To return the dynamic module as an object custom, you can also use the parameter -AsCustomObject of New-Module. Instead of being imported into the session, the members of the modules, like functions, are implemented in script methods of the custom objects.
Dynamic modules can only exist in memory and not on disk. Like all modules, dynamic modules have members that run in a private scope that is a child the global scope. Get-Module can’t get a dynamic module but Get-Command is able to get the exported members.
Pipe a New-Module command or the module object that NewModule returns to Import -Module to make a dynamic module available for Get-Module. This action adds the dynamic modules to the Get-Module List, but does not save them to disk or make them persistent.
What version of PowerShell do I use?
Get the PowerShell Version for your machine
This command displays the PowerShell version information for your machine.
How to use the New-Module
You can create a dynamic module by using Get-Module or Get-Command.
Line #1: new-module -scriptblock function Hello “Hello!”
Line 2: Get-Module
Line 3: Get-Command Hello
Line #1 in this example creates an entirely new dynamic module with a function called Hello.
Line 2 shows that dynamic modules are not returned from the Get-Module cmdlet.
Line #3 shows that members they export are returned to them by the Get-Command cmdlet.
Export a variable to the current session
New-Module-ScriptBlock $SayHelloHelp=”Type “SayHello”, a space and a name.”; function SayHello (“Hello, $name”) ; ExportModuleMember (function SayHello) -Variable SayHelloHelp
This example uses the Export ModuleMember cmdlet for exporting a variable into the current session. The function will not be exported if you don’t use the Export-ModuleMember command.
The output shows that both variables and functions were exported into the session.
Make a dynamic module available for Get-Module
New-Module -ScriptBlock function Hello “Hello!” -name GreetingModule | Import-Module
This example shows that you can make a dynamic modules available to Get-Module.
New-Module creates an object module that is piped to Import-Module cmdlet.
The -Name parameter in New-Module gives a friendly name for the module. This command does not produce any output because Import-Module doesn’t return any objects by default.
Get-Module indicates that the GreetingModule was imported into the current session.
The Get-Command cmdlet displays the Hello function that dynamic modules export.
Generate a custom object with exported functions
$m = New-Module -ScriptBlock function Hello ($name) “Hello, $name”function Goodbye ($name) “Goodbye, $name” -AsCustomObject
$m | Get-Member
This example shows you how to use the New-Module parameter -AsCustomObject to create a custom object with script methods that represent exported functions.
The New-Module cmdlet creates dynamic modules with two functions: Hello and Goodbye.
The -AsCustomObject parameter creates an object that is different from the default PSModuleInfo object that NewModule generates. This custom object is saved in $m variable.
The $m variable does not appear to have an assigned value.
The Get-Member cmdlet can be used to display the properties and methods for the custom object by adding $m.
The output shows that the object contains script methods that represent the Hello and Goodbye function. Finally, you can see that I call these script method and display the results.
The script block’s results are available here:
New-Module -ScriptBlock function SayHello “Hello, World!”; SayHello -ReturnResult
This example uses -ReturnResult to request the results of running a script block, instead of requesting a module.
The script block in this module defines the SayHello function, and then calls it.
Learn the Import-Module command from last week.
Do you need PowerShell training? ITProTV offers PowerShell online IT training.