How to clean up PowerShell Module repositories

Sometimes, you may get error message as follows and getting stuck on running PowerShell Cmdlets:

PackageManagement\Install-Package : Unable to install, multiple modules matched ‘AzureADPreview’. Please specify a
single -Repository…..
Uninstall-AzModule : The term ‘Uninstall-AzModule’ is not recognized as the name of a cmdlet, function…..
PackageManagement\Install-Package : No match was found for the specified search criteria and module name ‘AzureAD’. Try Get-PSRepository to see all available registered module repositories.”…

The single reason for all the above issues are you have deployed multiple repositories in your PowerShell user profile and the active one doesn’t have the commands supported. In our daily PowerShell experiments, we are trying out various modules and some of the active modules which are active are not installed correctly. Another reason is, you can’t uninstall an active PowerShell module which is used in your current session.

In all these situations, what I will do is uninstall the modules forcefully one by one by using the command as below. After each run, let’s check the active versions again and uninstall all of those one by one. Finally, install the latest stable version as below:

I saw 3 versions were installed as below:
Get-InstalledModule -Name Az -AllVersions

Removed one by one with the following cmdlet
powershell -NoProfile -Command “Uninstall-Module az”
Next time, if you run the above cmdlet (Get-InstalledModule -Name Az -AllVersions), you will see 2 active versions.

So continued the same uninstall process two ore times so that all modules are uninstalled completely. Finally, I am at the stage with no such modules are presented in my user profile.

Finally, Install the AZ module again
Install-Module -Name Az -Repository PSGallery -AllowClobber -Force
Now, I have a single repository only presented as below:

Finally Import the module by running the following cmdlet
Import-Module az
Hope, the method help you to resolve any PowerShell Module installation issues 🙂