More and more, you're seeing members of the Windows PowerShell community package their contributions into modules, myself included. Although you'll probably still see a lot of individual functions because it is often easier to demonstrate or educate. I received a comment on my Weather module that I thought merited a complete post, since I expect there are many administrators still new enough to PowerShell and especially PowerShell 2.0. The question is basically, "I've downloaded a module. Now what?"
ManageEngine ADManager Plus - Download Free Trial
Exclusive offer on ADManager Plus for US and UK regions. Claim now!
One major benefit of a module over snapins, is that modules can be deployed with a simple file copy. There is no installation, registration, or the like. Extract the files and PowerShell can use them. But where? Windows PowerShell 2.0 will examine a system environmental variable called %PSMODULEPATH%
[CC LANG="DOS"]
C:\Users\Jeff\>echo %psmodulepath%
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
[/CC]
The path you see here is for modules that would apply to any user of this computer. Open a PowerShell prompt and look at the variable and you will also see a path that is set for the current user.
[cc lang="powershell"]
PS C:\> $env:psmodulepath
C:\Users\Jeff\Documents\WindowsPowerShell\Modules;C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
[/cc]
The Modules directory does not exist by default, nor does WindowsPowerShell for that matter, so you might need to create them. Underneath the Modules directory you will have a folder for each module. Thus, you would extract my weather module to a folder called JH-Weather. In that folder are all the module files.
For the most part, these should be the only paths you need. But if you would like to specify an additional module path, you can modify the environmental variable by appending a new path.
[cc lang="powershell"]
PS C:\> $env:psmodulepath=$env:psmodulepath + ";c:\scripts"
PS C:\> $env:psmodulepath.split(";")
C:\Users\Jeff\Documents\WindowsPowerShell\Modules
C:\Windows\system32\WindowsPowerShell\v1.0\Modules\
c:\scripts
[/cc]
Now, PowerShell will also search C:\Scripts for any modules. HOWEVER..this modification is only for the current PowerShell session. If I open a new PowerShell session, I'll be back to the same path. If you always need this path, then add the modification command to your PowerShell profile script. Make sure you make the change in your profile before you attempt to import any modules from the new folder.
The Get-Module cmdlet will show all currently loaded modules. If you want to see all the available modules use the -ListAvailable parameter.
[cc lang=powershell]
PS C:\> Get-Module -ListAvailable | Sort Name | Select Name
Name
----
ActiveDirectory
AdminConsole
AppLocker
BitsTransfer
BSonPosh
DnsShell
DotNet
FileSystem
Godot7
GPAnswers
GroupPolicy
IsePack
JDHTools
JH-Weather
JH-WindowsUpdate
LocalUsersGroups
PowerShellPack
PSCodeGen
Pscx
PSDiagnostics
PSImageTools
PSRSS
PSSystemTools
PSUserTools
scripts
ShowTree
TaskScheduler
TroubleshootingPack
WebAdministration
WPK
[/cc]
When I want to use a module I import it into my session.
[cc lang="powershell"]
PS C:\> Import-Module WebAdministration
[/cc]
Use Get-Command to see the module contents.
Creating modules is a completely different topic for another day, but I hope this has helped get you started in the right direction regarding what to do with modules. If not, let me know. There is also a help topic, About_Modules you should read. Don Jones and I also cover modules in Windows PowerShell 2.0: TFM
As an FYI, you can also load a module independently of the module paths for a one-time use (or as part of your profile of course) by specifying the full path to the module folder with the Import-Module cmdlet.
Example: Import-Module C:\HyperV
This will import the module just as well as placing it in the appropriate Modules location. The limitation of this approach is that the module will not show up for the Get-Module cmdlet since it isn’t in the path so you have to of course know the full name and path of the module, but it’s handy for being able to access your modules from anywhere. I have a single Modules directory that I leave in a network share and have signed all modules that aren’t already using an internal CA. This way I can load any module on any server or system I access so long as I can reach my network share. It’s also handy for scripting since you can, for example, place a Modules dir in a DFS, or even SYSVOL if doing GPO scripting, and still reference it from anywhere so long as you have PoSh and access to the share.
Excellent point. Thanks.
Good info – I was just getting ready to explore this but you have saved me much time.
Thanks
Thanks, really good info.
I was wondering…is there a way prior to loading a module…that if there are other ‘dependancies’ that are being used within it? If one module depends on yet another module(which you don’t have loaded) is there a way to determine that upfront.
Checking dependencies is usually up to the module author. But if you’re curious about a module’s dependencies, that’s very difficult to get right now. You can only get detailed module information after it has been imported, which defeats the purpose. If the module is script based, you could search through the module files looking for import-module or add-pssnapin commands. Unfortunately, if it is a binary module you’re out of luck. Best to ask the developer for any dependencies.