How to clean up bad Azure PowerShell uninstall?

To go faster, you can uninstall in parallel:

workflow Uninstall-AzureModules
{
    $Modules = (Get-Module -ListAvailable Azure*).Name |Get-Unique
    Foreach -parallel ($Module in $Modules)
    { 
        Write-Output ("Uninstalling: $Module")
        Uninstall-Module $Module -Force
    }
}
Uninstall-AzureModules
Uninstall-AzureModules   #second invocation to truly remove everything

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.