Common issues with PowerShell while installing modules like MSONLINE, AzureAD | PowerShell Install-Module:The term ‘Install-Module’ is not recognized

ad+1

You want to connect to Microsoft 365 tenant with PowerShell to do some bulk operations like assigning licenses to users in bulk, change user properties, or just a simple password reset. You are about to begin your magic by installing your first PowerShell module and face this error  - not a good start, Duh!

PowerShell Install-Module:The term ‘Install-Module’ is not recognized as the name of a cmdlet, function, script file,or operable program.

Check the spelling of the name, or if a path was included,verify that the path is correct and try again.
At line:1 char :1
+Install-module msonline



You try to force the Install-Module cmdlet with -Force switch but meet another error. 😔

Install-Module MSONLINE -Force




What's missing? Why can't you install the PowerShell module?

Most likely, at least one out of below four is missing on your computer - 
  1. PowerShell Version
  2. TLS 1.2
  3. NuGet Provider Package
  4. PSGallery Repository 
Lets see them one by one. 

1. PowerShell Version

To install the powershell modules using install-module cmdlet, you need to run PowerShell 5.1 or higher. 

You can check the current version by simply typing HOST in the PowerShell console:

Get-Host



If the above cmdlet returns version lower than 5.1, you must install Windows Management Framework 5.1

To solve this - you can download it from Microsoft site here


2. TLS 1.2

Without TLS 1.2, you can't successfully Install NuGet Package Provider which is a prerequisite for installing new PowerShell Modules on a system.

You can check if your PowerShell has support for TLS 1.2 by running:


If you don't see the 'TLS12' in the list, you can enable it for the current PowerShell Session or permanently.

Enable TLS 1.2 in current session only - 

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12


Enable TLS 1.2 permanently- 

# Open Powershell (As Admin)
# Run the following cmdlets to set .NET Framework strong cryptography registry keys:

# Set strong cryptography on 64 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Wow6432Node\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

# Set strong cryptography on 32 bit .Net Framework (version 4 and above)
Set-ItemProperty -Path 'HKLM:\SOFTWARE\Microsoft\.NetFramework\v4.0.30319' -Name 'SchUseStrongCrypto' -Value '1' -Type DWord

Exit

# Open PowerShell again and check the current available TLS versions

[Net.ServicePointManager]::SecurityProtocol

Bonus: 

How can you spot the issue is TLS without running these cmdlets?
By looking at the error, which reads - 

PackageManagement\Install-PackageProvider : No match was found for the specified search criteria for the provider ‘NuGet’. The package provider requires ‘PackageManagement’ and ‘Provider’ tags. Please
check if the specified package has the tags.

3. NuGet Package Provider

Install-PackageProvider cmdlet installs modules available the PowerShell Gallery with the PackageManagement tag. PackageManagement requires to download the NuGet package provider. 

You can check if NuGet is already available on your machine by running:

Get-PackageProvider


If NuGet package provider is not installed, install it by running below cmdlet in an elevated PowerShell window:

Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force

4. PSGallery Repository

If the error is due to incorrectly installed or not installed PSRepository, the error may look like:
The error can also look slightly different i.e.

PS C:\WINDOWS\system32> Install-Module -Name MSOnline
PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'MSOnline'.
Try Get-PSRepository to see all available registered module repositories.
At C:\Program Files\WindowsPowerShell\Modules\PowerShellGet\1.0.0.1\PSModule.psm1:1809 char:21
+ ...          $null = PackageManagement\Install-Package @PSBoundParameters
+                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (Microsoft.Power....InstallPackage:InstallPackage) [Install-Package], Ex
   ception
    + FullyQualifiedErrorId : NoMatchFoundForCriteria,Microsoft.PowerShell.PackageManagement.Cmdlets.InstallPackage

To solve this Run the Get-PSRepository cmdlet to confirm the PSGallery PSRepository is installed on the system. 
The output should look like below: 

Get-PSRepository

If not, Restore the default repository by using the command

Register-PSRepository -Default

Bonus: 

How can you spot the issue is PSRepository without running these cmdlets?
By looking at the error, which reads - 

PackageManagement\Install-Package : No match was found for the specified search criteria and module name 'MSOnline'.

The problem is now solved! You can install PowerShell Modules now!

Install-Module MSONLINE -AllowClobber -Force


Enjoy!

0 comments:

I welcome you to write your comments here..