Windows PowerShell V2 Modules
- Posted: Apr 09, 2009 at 12:41 PM
- 6,106 Views
- 1 Comment
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
Right click “Save as…”
This video demonstrates converting a Windows PowerShell script to a Windows PowerShell module.
See more info at MSDN Code Gallery.
The video starts by introducing a simple script file, counter.ps1, with three functions and two variables.
The script file is dot sourced into the environment. All variables and functions are now in scope. The intent is to only have two of the functions in scope, with the rest being private implementation details.
Next, the file is renamed from counter.ps1 to counter.psm1 for the correct module extension. Using the Import-Module cmdlet, the module is loaded into scope. It is demonstrated that the variables are private, however all functions are public. This is shown using Get-Command -module counter to list the exported commands.
Before changing the module it is necessary to remove it from scope. This is accomplished using Get-Module | Remove-Module to pipe any modules (only one in this sample) to be removed.
In order to hide certain functions, it is necessary to specific which are public using Export-ModuleMember function_name. By specifying only the intended public functions, others remain private, only accessible from within the module.