Uninstalling all versions of Azure PowerShell

I hit an interesting snag with the Azure PowerShell Module this week.

It turns out that during a bit of troubleshooting before Christmas, I had deliberately installed multiple versions of the package - to cut a long story short, I thought that another engineer's problems were caused by him doing the same, and so I tried to recreate it. It wasn't in fact the problem, but I didn't bother cleaning up after myself.

When you install the module for the first time, it's relatively straightforward:

Install-Module AzureRM 

but there's a downside to this simplicity. The 'AzureRM' package is just a wrapper that has dependencies on all of the other AzureRM modules. As a result, trying to update or uninstall this package works, but doesn't have the expected effect:

> Uninstall-Module AzureRM
> Get-InstalledModule

Version Name
------- ----
0.5.0   Azure.AnalysisServices
4.0.2   Azure.Storage PSGallery Microsoft Azure PowerShell - Storage service
0.6.1   AzureRM.AnalysisServices PSGallery Microsoft Azure PowerShell - Analysis
5.0.1   AzureRM.ApiManagement PSGallery Microsoft Azure PowerShell - Api 0.1.0
4.1.1   AzureRM.Automation PSGallery Microsoft Azure PowerShell - Automati
...


None of the actual worker modules have been removed!

It gets worse if you update the package. You will end up with multiple versions of the worker modules installed, and eventually you'll end up (like I did this week) with a conflict.

So how can you tell if you have multiple versions installed? Well the Get-InstalledModule cmdlet has a useful AllVersions parameter, but it will only work if you specify the name of a single module. We'll have to use a pipe and a foreach:

Get-InstalledModule AzureRM* | % {Get-InstalledModule $_.Name -AllVersions}


This will list all installed versions of the AzureRM modules. It should be pretty obvious if there are any duplicates, but a quick test is to find AzureRM.profile; since almost every other module depends on it, it is more likely than anything else to get duplicated.

...
4.1.1 AzureRM.PowerBIEmbedded  PSGallery Microsoft Azure PowerShell - Power BI
3.4.1 AzureRM.profile          PSGallery Microsoft Azure PowerShell - Profile
4.1.1 AzureRM.profile          PSGallery Microsoft Azure PowerShell - Profile
4.0.1 AzureRM.RecoveryServices PSGallery Microsoft Azure PowerShell - Recovery...

...

I would recommend that for simplicity's sake, it's best to clear out and start again when a new version of the module is released. This can be done with a simple modification to the above command line.

First close all existing PowerShell and ISE windows, then open a new one as Administrator. The following command will uninstall all versions of any installed modules:

Get-InstalledModule AzureRM* | % {Uninstall-Module $_.Name -AllVersions}


Then just run Install-Module AzureRM again and everything will be back to normal.

Let me know in the comments below if you have any questions or queries. Topic ideas for future blogs are also welcome - if you want to know how to bend PowerShell or Azure to your will, please ask!

Comments