Installing Modules

Installing Modules

Whether you’re writing scripts, functions to automate a repetitive task, or building a whole toolkit you should be familiar with installing modules. Modules let you group functions in a logical way. Creating a bunch of functions that automate part of your Active Directory environment? Group them all into a module. Modules help by letting you keep track of the version you have installed on your machine as well as allowing for automatic loading into the current session.

Module Contents

What makes up a module. At the basic level, all you need is a .psm1 file with a function in it. From there, you may have a module manifest file (.psd1) file that tracks the meta-data of your module (name, version, description, etc). There can be help files, formatting files, as well as DLLs to help you complete the job.

Install Modules via File Copy

When installing modules from the internet or making your own you should be storing them in certain folders to make sure every PowerShell console window you open has access to them. Running Import-Module -Path “C:\FakePath.psd1” isn’t a scalable solution. There is an environment variable baked into PowerShell that determines where it looks for .psm1 and .psd1 files.

$env:PSModulePath -split ";",""

$Env:PSModule path will return a list of directories PowerShell will search for modules. The split command simply puts them all on their own line after splitting by the ‘;’ delimiter. Put your module in one of these folders and any PowerShell console v3 or higher will automatically load your module for you.

module structure

Note: You have to match the folder name to the .psm1 or .psd1 file, otherwise the console will not find the module.

Install Modules via Cmdlets

With Windows 10 Microsoft shipped with a module repository called the PowerShell Gallery. Both Microsoft and the community could upload their modules to a place that can be downloaded from the console.

Module Install

The beauty of this method is that you don’t even need to leave the PowerShell console! You can also grab the latest version of any installed modules very easily.

Get-InstalledModule | Update-Module -force

Private Repository

If you work for an enterprise and don’t want your code stored off-site or if you just don’t like the idea, you can host your own PowerShellGet repository using IIS. Boe Prox put together a great guide on setting up a PowerShellGet (formally know as OneGet) repository for your site. Setting Up a NuGet Feed For Use with OneGet (now PackageManagement). This is the guide I followed to setup a repository at my work for easily sharing my modules with co-workers.

Leave a Reply

Your email address will not be published. Required fields are marked *