Writing a simple module
A module, at its most basic, is a script file containing functions, saved with a .psm1
extension. That’s it. That’s the simplest possible module. Try it – save the following lines as a .psm1
file, in a folder with the same name as the file, inside your \users\<username>\documents\powershell\modules
folder (or the home/<user>/.local/share/powershell/Modules
folder in Linux):
function Get-Square($a) { $result = $a * $a return $result }
It doesn’t matter what you call the file, so long as the file and folder name are the same, and the folder is in the module path so that PowerShell can find it, like this:
Figure 11.2 – Saving a module in the module path correctly
Now, if we start a PowerShell session, we can type the following:
Import-Module <ModuleName>
PowerShell will load it. Once it is loaded, we can use the functions...