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).

This blog is part of Azure Week. Check it out for more great content!

About the Author:

I am a not so typical technologist. My roots are grounded in public speaking and writing. I was a Communication Studies major and English minor in college, plus I participated in competitive high school speech tournaments prior to collegiate life. I wound up gravitating toward technology through music, by way of being a DJ and needing to record demos for club promoters. By taking the time to develop initial skills of troubleshooting, I quickly found myself in jobs that started to graduate me toward deeper levels of technical skill. A self-described tinkerer, I have worked with technical mentors and colleagues who have helped me develop proficiencies in infrastructure. From my initial roots in help desk, to Windows systems administration, to VMware administration, to Exchange, to Exchange Online/Office 365, and finally Azure, I have carefully combined the right levels of technical expertise that propelled me into my role at Microsoft.  From Cloud Architect to Cloud Advocate, my time at Microsoft has been a whirlwind ride. I am currently a Cloud Advocate on the Enterprise Platforms and Tools team. I focus on community outreach and content creation for platforms enterprises consume and use. Our first product we’ve spent time on is the Azure VMware Solution platform (plus everything that bolts onto the service) as a way to get IT pros comfortable with a life in the cloud.

Reference:

Kuehn, S. (2021). Azure PowerShell – Complete Uninstall to Reinstall. Available at: https://www.shankuehn.io/post/azure-powershell-complete-uninstall-to-reinstall [Accessed: 7th July 2021].

Share this on...

Rate this Post:

Share: