13

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!

Mark Maslar
  • 1,121
  • 4
  • 16
  • 28

7 Answers7

38

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.

Community
  • 1
  • 1
  • One tip! This script worked for me (working on Mac/Powershell core) as the deletions took affect....but after I ran the Az specific commands there was still some fuzziness and seemed to be some errors. (ie. Az still referencing some AzureRM files which were fully deleted if you double checked the file system.) I simply restarted the Terminal window(s) ... problem solved. – Andrew Chung Apr 17 '19 at 17:43
25

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
BlueSky
  • 1,449
  • 1
  • 16
  • 22
2

It's just a Dev VM. I nuked it and started over. Lesson learned: Uninstall PowerShell Gallery components before uninstalling the MSI.

Mark Maslar
  • 1,121
  • 4
  • 16
  • 28
1

Try uninstalling the modules via MSI (first) and then cmdline:

# Uninstall the AzureRM component modules
Uninstall-AzureRM

# Uninstall AzureRM module
Uninstall-Module AzureRM

# Uninstall the Azure module
Uninstall-Module Azure

# Or, you can nuke all Azure* modules
# Uninstall-Module Azure* -Force

Reboot the machine after that and then install again via WebPI/MSI. https://azure.microsoft.com/en-us/blog/azps-1-0/

bmoore-msft
  • 8,376
  • 20
  • 22
  • I couldn't run any Azure PowerShell commands from cmdline because Azure PowerShell was no longer functional.So no cmdline & no ISE! – Mark Maslar Dec 16 '15 at 01:42
  • 1
    The Uninstall-Module (not uninstall-AzureRM) cmdlet is not part of the Azure cmdlets, it's a generic PS cmdlet. Try those commands - note those will only work if you installed via the gallery - if you installed via MSI, you need to uninstall via the MSI. – bmoore-msft Dec 20 '17 at 15:43
1

I created this variation on @BlueSky answer to keep one specific version and its dependencies:

workflow Uninstall-AzureModules
{
    $skip = Find-Module AzureRM -RequiredVersion 2.1.0
    $dependencies = $skip.Dependencies | %{ Get-Module -FullyQualifiedName @{ ModuleName=$_["Name"]; RequiredVersion=$_["RequiredVersion"] } -ListAvailable }

    $modules = Get-Module azurerm* -ListAvailable | 
        where { $_.Version -ne '2.1.0' } |
        where { -Not(Compare-Object $dependencies $_ -IncludeEqual -ExcludeDifferent) }

    foreach -parallel ($module in $modules)
    { 
        Write-Output ("Uninstalling: $($module.Name) $($module.Version)")
        Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -Force -ea SilentlyContinue
    }
}
riezebosch
  • 1,950
  • 16
  • 29
0

I found the command Get-InstalledModule works better for me for discovering my installed modules.

My latest script looks like this:

# If installed, remove the old AzureRm module
$allModules = Get-InstalledModule
foreach ($module in $allModules)
{
    if ($module.Name -match "AzureRM.*")
    {
        Write-Host "Removing $($module.Name)..."
        Uninstall-Module -Name $module.Name -Force -AllVersions
    }
}

# If not installed, install the new Az module
$azModule = Get-InstalledModule -Name Az
if (!$azModule)
{
    Install-Module -Name Az -Scope CurrentUser -AllowClobber -Force
}

# Enable AzureRm aliases for script compat
Enable-AzureRmAlias -Scope LocalMachine
0

This did not work in my case. I am on Powershell v7.02 MACOS v10.15.6. What did work was this code:

Function Uninstall-AzureModules
{
    $Modules = (Get-Module -ListAvailable Azure*).Name |Get-Unique
    Foreach ($Module in $Modules)
    { 
        Write-Output ("Uninstalling: $Module")
        Uninstall-Module $Module -Force
    }
}
Uninstall-AzureModules
Uninstall-AzureModules

I them verified the AzureRM modules as gone with:

Get-InstalledModule -Name AzureRM*
double-beep
  • 5,031
  • 17
  • 33
  • 41