Install-Module is a wonderful new cmdlet that comes with PowerShell v5 and can be found in Windows Management Framework (WMF) 5.0. This allows us to skip the whole search the Internet to find modules and pull them from pre-configured repositories. By default, your machines should be configured to look at https://www.powershellgallery.com/api/v2/ .
Why use Install-Modules
It makes your life easier! Instead of going to the Internet, searching, downloading the files and then putting them into the appropriate directories, you can use Install-Module. Doesn’t that just make your life easier?
But again Why? What modules are out there that I should or could be using? I’ll give two primary examples where I use this all the time, Microsoft Azure and Desired State Configuration (DSC). There are so many Microsoft teams that provide supported PowerShell modules for you to download and use, similarly there are many 3rd party and individuals who also author their own modules to help out the community.
Did someone in the community just release an update? Now we can just Update-Module and we’re done!
You would think that running the new command would be easy and it would just work. In most cases it does, but in my experience you’ll be prompted a few times to install things here and there. Underneath the covers Install-Module uses the module, PowerShellGet. PowerShellGet uses a Package Provider provided by Microsoft call NuGet. I hope you’re not lost yet! That should be as deep as we need to go to see what is actually going on when we use Install-Module.
By default, the NuGet Package Management provider is not installed by default. You can install it by running:
Once we install the NuGet Package Provider, we’re ready to go ahead and use the commands provided by the PowerShellGet module.
Before running and installing modules, let’s just look at where we, or I should say the system looks for modules. These modules are stored in repositories outside of your organization or home.
Listing the configured repositories, run the Get-PSRepository cmdlet. To demonstrate a little more, let’s run:
Get-PSRepository| ft Name, InstallationPolicy, SourceLocation, PackageManagementProvider
Notice that we are using NuGet behind the scenes and connecting to www.PowershellGallery.com.
Jump over to this blog to see how you could create your own NuGet Respository. https://blogs.msdn.microsoft.com/powershell/2014/05/20/setting-up-an-internal-powershellget-repository/
Finding Modules in the Repositories
Now that we know which repositories are configured and where the system will look, how do we know what’s available at each the repositories.
We can surf around on the PowerShellGallery.com website, but let’s use PowerShell. Introducing the Find-Module cmdlet. What a tricky name!
This can take a wildcard characters in the name that are similar to the -like search (Ex. “*”). Here is an example of finding all the Desired State Configuration (DSC) modules for System Center.
There are the -RequiredVersion or the -MaximumVersion and -MinimumVersion parameters. Using these parameters will help the Find-Module cmdlet install the correct one you’re looking for. These are actually a couple good options to think about sometimes, especially when you’re working in an environment with strict change controls and you need to keep a very specific version of a module.
Before we go ahead and use these Maximum and MinimumVersion parameters, let’s use another parameter to show us all the version that are available to us. Unfortunately, all 3 of these parameters only work on a single module name. So let’s take a look at Azure module.
Find-Module -Name Azure -AllVersions
Now that we know what version are available, let’s try the MaximumVersion and MinimumVersion parameters.
Find-Module -Name Azure -MinimumVersion 1.3.0 -MaximumVersion 2.0.0
Now let’s try the RequiredVersion Parameter. This requires an explicit match to be found, otherwise an object is not returned.
Find-Module -Name Azure -RequiredVersion 220.127.116.11
Installing a Module
Now that you’ve found a module to install, let’s install it. Of course we already know the cmdlet is call Install-Module and yes it’s pretty simple.
Install-Module -Name Azure -RequiredVersion 18.104.22.168
Let’s see the installation outcome.
Get-Module -Name Azure –ListAvailable
Updating the Module
Now that we have a version installed, let’s pretend that someone just upgraded the Azure module from 22.214.171.124 to something else. Let’s go ahead and update it.
Update-Module -Name Azure
Let’s check the result.
Get-Module -Name Azure –ListAvailable
Did you notice that we now have two version of the module? So what happens now? Well the Import-Module will use the latest version. If you want to use a specific version, you’ll have to use the –Version parameter along with the Import-Module cmdlet.
One of the nice things that I really love, is that these cmdlets support using a proxy. Have to log working in security conscious environments. Use the –Proxy and –ProxyCredential to get out to the Internet world!
Well I hope I’ve given you a little glimpse and ideas into managing your PowerShell Modules. Until next time!