top of page
Search
  • Writer's pictureShannon

Azure PowerShell - Complete Uninstall to Reinstall

I know a lot of us are using more command line based tools these days. Azure PowerShell is one of the tools I tend to use often if I need to provision infrastructure quickly. When the cmdlets moved from AzureRM to Az, a number of us had problems with a borked machine that couldn't authenticate to Azure or provision any resources. Then subsequent updates may have caused some problems.


So what do you do if Azure PowerShell doesn't do things like authenticate, provision resources, etc.? The idea is to completely uninstall the Azure PowerShell modules. Unfortunately command prompts with authenticated sessions or something like Visual Studio code or PowerShell ISE will cause some friction if left open during the process of uninstallation. You may also find that modules just won't uninstall.


I wrote a quick PowerShell script that I use as part of my computer builds/automations when things don't work quite right related to Azure and PowerShell. Feel free to take/borrow/tweak and make it work for you!

AzureRM (if you find this installed anywhere):

ForEach ($module in (Get-Module -ListAvailable AzureRM*).Name | Get-Unique) {
    Write-Host "Removing Module $module"
    Uninstall-Module $module -Force
}

Az:

ForEach ($module in (Get-Module -ListAvailable Az*).Name | Get-Unique {
    Write-Host "Removing Module $module"
    Uninstall-Module $module -Force
}

Additionally, I have this PowerShell script highlighted in my GitHub repo called Azure Operations: CleanUpPoSH.ps1


I have also used the following PowerShell workflow script a few times when the Azure PowerShell modules have been hard to remove:

workflow Uninstall-AzureModules
{
    $modules = (Get-Module -ListAvailable Az*).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

I have this script living on GitHub as well (note, you can switch out Az* to AzureRM*): workflowCleanUpPoSH.ps1


Hopefully both of these scripts help if you have something not working right with PowerShell on your machine for Azure operations. These scripts have saved me a few times over from completely reimaging my machine (because something was completely tripped up).

531 views0 comments

Recent Posts

See All
bottom of page