Introduction
Modules help you keep useful code in one place so you can use it again and again without rewriting it.
Modules help you keep useful code in one place so you can use it again and again without rewriting it.
Import-Module -Name ModuleName Export-ModuleMember -Function FunctionName
Import-Module loads the module so you can use its functions.
Export-ModuleMember makes functions available outside the module.
# In MyModule.psm1 function Get-Greeting { param($Name) "Hello, $Name!" } Export-ModuleMember -Function Get-Greeting
Import-Module -Name MyModule
Get-Greeting -Name "Alice"This example shows how to create a simple module with a greeting function, import it, and use it.
# Create a module file MyModule.psm1 with this content: function Get-Greeting { param($Name) "Hello, $Name!" } Export-ModuleMember -Function Get-Greeting # Then in your script or PowerShell session: Import-Module -Name .\MyModule.psm1 Write-Output (Get-Greeting -Name "Bob")
Modules keep your code clean and easy to manage.
Always export only the functions you want others to use.
You can have many modules for different tasks.
Modules store reusable code in one file.
Import modules to use their functions in your scripts.
Export only the functions you want to share.
MyTools in PowerShell?Import-Module followed by the module name.MathTools.psm1 with content:function Add-Numbers { param($a, $b) return $a + $b }Import-Module .\MathTools.psm1
Add-Numbers 3 4
Add-Numbers is available.Get-Data and Set-Data but hides others. How do you do this?Get-Data and Set-Data to share them and hide others.Pass the test and this pattern moves unknown