How to clean up bad Azure PowerShell uninstall?

Mark Maslar picture Mark Maslar · Dec 10, 2015 · Viewed 22.7k times · Source

Background: I had installed Azure-PowerShell 1.x via the MSI, and subsequently added some Azure Resource Management modules via the command prompt. Everything was working fine; then yesterday afternoon the ISE inexplicably disappeared. In an attempt to remedy that, I planned to uninstall the MSI & then reinstall. (I did not know that it is necessary to first unstall the manually-added modules.) The uninstall appeared to run fine, but it didn't remove the manually-installed modules nor did it warn me about them.

Result: The machine no longer has Azure-PowerShell installed. I cannot Install, Uninstall or Repair the installation because some modules remain:

Azure Modules from the PowerShell Gallery are installed on this machine. Please remove these modules before installing this MSI.

Is there a way to "fix" this installation? Either manually remove files/cleanup registry, or force the MSI to install over whatever is there?

This is all running on a VM on Azure. I could delete the entire VM & start from scratch if necessary, but would prefer to avoid that if there's a relatively simple fix.

Thanks!

Answer

Isaac Kane Egglestone picture Isaac Kane Egglestone · Nov 6, 2017

Okay so I tried the above items to remove windows powershell and found that actually it doesn't fully remove powershell.

This is at least on windows 7 it doesn't seem to properly.

If you run uninstall-module Azure or Uninstall-Module AzureRM it will uninstall something, looks like the base module I think.

Then if you do:

Get-Module AzureRM -ListAvailable

it will come back with nothing. So its done right?

No not really.

If you then do:

Get-Module -ListAvailable AzureRM*

you will find any number of sub modules still sitting there. (For some reason wildcards work with Get-Module but not with Uninstall-Module)

Okay so but then just do Uninstall-Module AzureRm* right? No not really

Depending on your powershell version (Or maybe not, I'm not sure) Install-Module just complains that you can't use wildcards in the Uninstall-Module command. (God knows why what's the point of wildcards if not for this?, but then this is windows so I just had to suck it up).

And if you take a look in %System-root%\Program Files\windowspowershell\modules you will still see the modules there.

Why is this? I'm not sure but this is what I had to do to cleanup all the old versions and newer versions of Azure powershell that I had to get back to a clean slate. So to solve the problem of lack of wildcard support I just used a foreach loop as such:

foreach ($module in (Get-Module -ListAvailable AzureRM*).Name |Get-Unique) { write-host "Removing Module $module" Uninstall-module $module }

Be warned Don't try to run this as Visual Studio code or visual studio for that matter, as depending on your lock you may get errors as it tends to pre-load the modules and lock things open. Put it in a file named Removeold-AzureRM.ps1 and run it from a powershell console window like this: .\Remove-AzureRM.ps1

Also Make sure to close down Visual Studio Code and Visual studio before attempting it or you may still get a similar error message.

If you run this after you already uninstalled AzureRM you will find that things stop working and you only have one last resort. Delete all the AzureRM modules in %System-root%\Program Files\windowspowershell\modules

Edit I have re-tested this and the code above still seems to work if you have azurerm 5.0.1 installed and you already removed azurerm so I could be wrong about other versions as well

Now you will at this point for sure have this sorted and you can now reinstall Azure powershell with Install-Module AzureRM.

If you made the mistake of nuking powershellget like me by accident, don't bother trying to reinstall it with WMF 5.1 or 5.0 as that will install fine but you still won't have powershellget, why I'm not sure and again this is windows so lets just suck it up.

Okay so how to fix it then?

Your only recourse is to download the release of powershellget

And and copy the PowerShellGet-1.5.0.0\PowerShellGet to your modules folder. Then Install-Module will work again.

Yeah I know we are all saying isn't it just safer to re-install?

Yes likely but for those of you like me where that was not an option for one reason or another the above is your best bet. I hope this helps someone as this took me at least 3 days to sort out why I kept getting older modules executing when I was sure I had already removed everything.