How to Install PowerShell Modules in Windows 10 & Server 2016

It’s no secret that PowerShell is a very powerful and useful tool. That usefulness, though, can be expanded by adding modules such as Active Directory and Office 365 modules. There are even non-Microsoft modules that can be installed such as VMWare’s PowerCLI, which let you take administrating vSphere to another level.

Though, I’m going to go out on a limb and guess that because you’re here, you already know this and you want to know HOW to install those modules.

Am I right?

Well let’s jump in then!

Downloading and Installing PowerShell Modules

There are several ways to install PowerShell modules but we are going to look at the easiest way first. The basic steps are to determine your current PowerShell module directory path, download your new module to that path, and then invoke the import-module command to let windows know it’s there.

Let’s see it in action.

List PowerShell Module Environment Variable Path

We need to determine the install path for our PowerShell Modules so we know where to put new modules. To do this, open a PowerShell window and run the following command:

$ENV:PSModulePath

The output should look similar to:

PS C:\WINDOWS\system32> $ENV:PSModulePath

C:\Users\UserName\Documents\WindowsPowerShell\Modules;C:\Program Files\WindowsPowerShell\Modules;C:\WINDOWS\system32\WindowsPowerShell\v1.0\Modules

You’ll see two important paths here:

C:\Users\UserName\Documents\WindowsPowerShell\Modules
C:\Program Files\WindowsPowerShell\Modules

Make a mental note of these paths, we’ll use one or both of them later. The first path is that path you would place a new PowerShell module that you only want that specific user to have access to. The second path would make the module available for all users on the computer.

The third path we received with the $ENV:PSModulePath command is the path Windows uses for built-in modules that are already installed when you install Windows.

Download New PowerShell Module to Environment Variable Path

Armed with the path we wish to use (from above) we can source our PowerShell Module and copy it to the location specified in the path.

Here I’ve downloaded the PowerNet PowerShell module from CodePlex and placed it in the directory that will allow all users on the computer to load the module.

I then run the following command to make sure the module is listed as available:

Get-Module -ListAvailable

Which gives me the following output, where we can clearly see the PowerNet module listed:

PS C:\WINDOWS\system32> get-module -listavailable

    Directory: C:\Program Files\WindowsPowerShell\Modules

ModuleType Version    Name                                ExportedCommands
---------- -------    ----                                ----------------
Script     1.0.1      Microsoft.PowerShell.Operation.V... {Get-OperationValidation, Invoke-OperationValidation}
Binary     1.0.0.1    PackageManagement                   {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...
Script     3.4.0      Pester                              {Describe, Context, It, Should...}
Script     0.0        PowerNet                            {Test-TCPPort, Enable-Monitoring, Disable-Monitoring, Writ...
Script     1.0.0.1    PowerShellGet                       {Install-Module, Find-Module, Save-Module, Update-Module...}
Script     1.2        PSReadline                          {Get-PSReadlineKeyHandler, Set-PSReadlineKeyHandler, Remov...

Import New PowerShell Module

With our new module in place we just need to tell Windows to load it so that we can use it in our current PS Session.

To this, I will run this command:

Import-Module -Name PowerNet

You may receive the following warning:

Security warning
Run only scripts that you trust. While scripts from the internet can be useful, this script can potentially harm your
computer. If you trust this script, use the Unblock-File cmdlet to allow the script to run without this warning
message. Do you want to run C:\Program Files\WindowsPowerShell\Modules\PowerNet\PowerNet.psm1?
[D] Do not run  [R] Run once  [S] Suspend  [?] Help (default is "D"):

Type R to run once, assuming you’re sure this is a safe module.

If you receive the following error when trying to load your module:

import-module : File C:\Program Files\WindowsPowerShell\Modules\PowerNet\PowerNet.psm1 cannot be loaded because running scripts is disabled on this system.

You may need to run the following command to allow loading the module:

Set-ExecutionPolicy Unrestricted

Be sure to set it back to restricted when you’re finished, using the following command:

Set-ExecutionPolicy Restricted

It should be obvious, but I’ll state it anyway, be careful loading PowerShell modules from the internet. These are scripts and can contain malicious code. Only load modules you trust and that are from a trusted source.

With that said, we are finished with this method of loading PowerShell Modules. Try it out and let me know if you have any trouble with it!

 

Install PowerShell Modules from PowerShell Gallery

The other way we can install PowerShell modules is to use the PowerShell Gallery. These will be modules that have been authored by Microsoft and the PowerShell community. You’ll install them through PowerShell directly from the internet. That also means you’ll need to make sure that your firewall and content/web filter allow connections from your computer to the internet through PowerShell. I won’t get into that here, though.

The process is simple but we need to step through a couple prerequisites first.

Verify You Have the PowerShellGet Module

Windows 10, Windows Server 2016, Windows Management Framework (WMF) 5.0, and PowerShell 6 are all shipped with PowerShellGet. If you’re running any of those you’re good to go here.

If you are running an OS older than Windows 10 or using a PowerShell older than 6.0 you’ll need to download the PowerShellGet msi here.

Update Nuget

PowerShellGet relies on the provider named Nuget. Always start with the latest version by running the following command:

Install-PackageProvider Nuget –Force

Update PowerShellGet

We also need to make sure your PowerShellGet install is on the latest update. Do this by running:

Install-Module –Name PowerShellGet –Force

Install PowerShell Module from Gallery

To install a module we just need to know it’s name. The gallery page will tell you what the command is to install the module. I’ve found a module on the gallery named SqlServer that I wish to install so I’ll run the following command:

Install-Module -Name SqlServer

After which I receive the following message:

Untrusted repository
You are installing the modules from an untrusted repository. If you trust this repository, change its
InstallationPolicy value by running the Set-PSRepository cmdlet. Are you sure you want to install the modules from
'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

I will answer Y which will commence installing the module which I can see visually by the loading text at the top of the window:

And that’s it! Pretty slick huh? Try it out and let me know if you have any issues!

Recommended for You: Solarwinds Server & Application Monitor (SAM)

Know which applications are having issues in your environment before users complain? Know which systems are causing those problems? How about which servers are about to have problems like running out of space or memory?

Automate collection of data and alerting on your applications and servers with Solarwinds Server & Application Monitor so you have these answers.

Get insight into Active Directory, DNS, DHCP, and your Virtual environment without needing to mess with complex templates or knowing a single line of code.

Leave a Reply

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