Introduction to PowerShell Modules
PowerShell modules are like mini-apps, they have instructions to deliver useful gismos. Configuring the path for the Import-Module cmdlet requires moderately complex preparation, and the purpose of this page is just get readers started.
PowerShell Modules Topics
- Creating the Special PowerShell Module Folder
- Import-Module
- Get-Module
- Load Modules with a PowerShell Profile
Creating the Special PowerShell Module Folder
You need a known path where your .psm1 modules are stored. I would start by finding $Profile. Then try PSProfilePath.
# PowerShell profile
Clear-Host
$Profile
$PSProfilePath
Import-Module
We need to start with the trickiest member of the module family ‘Import’. There are two factors to ensure success of my experiment:
a) Creating a simple test module containing PowerShell commands and with a .psm1 extension.
b) Making a note of the path to your saved .psm1 file.
Create a Test Module
Here is our test module, it has two functions ‘Grow’ and ‘Shrink’.
I suggest we call the file: Balance.psm1
Now for the key part, the path; I suggest saving this file to:
C:\Users\YourName\Documents\WindowsPowerShell\Modules
function Grow {
if(!(Test-Path variable:script:count)) { $script:count = 0 }
$script:count++
"This is where Guy adds one {0}!" -f $script:count
}
function Shrink {
if(!(Test-Path variable:script:count)) { $script:count = 0 }
$script:count–
"Now Eddie takes one away {0}!" -f $script:count
}
Import and Test
Once the PowerShell module directory is ready then you can import the code.
Clear-Host
$Location="C:\Users\YourName\Documents\WindowsPowerShell\Modules"
Import-Module $Location\Balance.psm1
When the module loads sucessfully, two functions are available; to tes them type: ‘Grow’, or ‘Shrink’.
Note 1: I introduced the $Location variable merely to highlight that you need to amend this value to suit your profile, e.g. YourName is not the correct folder. Incidentally, it seems to me that Import-Module needs the full path.
Note 2: Just to emphasise, you do need to create a file called ‘Balance’ with a modules extension (.psm1) for this to work.
Guy Recommends: A Free Trial of the Network Performance Monitor (NPM) v11.5
SolarWinds’ Network Performance Monitor will help you discover what’s happening on your network. This utility will also guide you through troubleshooting; the dashboard will indicate whether the root cause is a broken link, faulty equipment or resource overload.
What I like best is the way NPM suggests solutions to network problems. Its also has the ability to monitor the health of individual VMware virtual machines. If you are interested in troubleshooting, and creating network maps, then I recommend that you try NPM now.
Download a free trial of Solarwinds’ Network Performance Monitor
Get-Module
This displays the added, loaded, or to use the correct verb, imported modules; in this example you should see:
ModuleType Name ExportedCommands
———- —- —————-
Script Balance {Grow, Shrink}
Note 3: If this ‘Balance’ module has loaded then you will be able to use the functions ‘Grow’ and ‘Shrink’.
Reminder of The Recommended PowerShell Module Directory
<Documents folder Path>\WindowsPowerShell\Modules\<Module Folder>\<Files>
The module manager should honour scripts marked with the appropriate Zone Identifier, as a result the modules conform the PowerShell script execution policy.
Loading Modules Automatically Through Profile
Individual user’s profiles, and programs such as ‘Word’ each have a ‘Startup’ folder; in the case of PowerShell, we can access it via the $Profile variable. Actually, there are two profile files, one for PowerShell’s ISE and another for the plain command-line PowerShell. Fortunately, you can launch notepad and edit either of them.
The Plan
Open Microsoft.PowerShell_profile with notepad
Append an Import-Module command
See details of how to modify a PowerShell Profile.
See All Members of the Modules Family
Clear-Host
Get-Command -Noun module
Results:
- Get-Module
- New-Module
- Import-Module
- Remove-Module
See more examples of Import-Module »
Summary of PowerShell Modules Directory
The Import-Module cmdlet works nicely when it has the correct path configured. The secret is to research the location of $Profile and $PSProfilePath. As for the big picture, PowerShell modules are like apps.
If you like this page then please share it with your friends
See more Microsoft PowerShell tutorials
• PowerShell Tutorials • Methods • Cmdlets • PS Snapin • Profile.ps1 • Exchange 2007
• Command & Expression Mode • PowerShell pipeline (|) • PowerShell ‘where‘ • PowerShell ‘Sort’
• Windows PowerShell Modules • Import-Module • PowerShell Module Directory
If you see an error of any kind, do let me know. Please report any factual mistakes, grammatical errors or broken links.