A module is a logical separation of functions and cmdlets.
For example, you can group cmd-lets dealing with data compression in one module, and cmdlets dealing with network operations in
another module.
You can watch me code.
[embed]https://youtu.be/mI6K7RbQTrA[/embed]
To create a new module in powershell, the file should end with extension of .psm1.
That is the requirement.
So, open a text editor and type these lines.
function Get-ChannelName {
return "Refactored Codes";
}
This function/cmd-let prints the name of my youtube channel.
After typing the code save the file as first-module.psm1.
Now, the next step is to use Get-ChannelName in our commandline.
But, if you run it now, you will get an error, as this cmdlet is not available in the current session.
To make it available, we use Import-Module to add the contents of the module in the current powershell session.
Run this command, and you have to provide path to the module.
Import-Module .\first-module.psm1
Once the module has been imported you can run Get-ChannelName cmdlet successfully.
We can add more functions to the module file.
But, to see/refresh the latest changes.
- first remove the module using
Remove-Module first-module
- then import the module again using
Import-Module .\first-module.psm1
This concludes this chapter.
Thanks for visiting.