Custom PowerShell function to remove Azure Module

As you probably know by now, “Azure RM” modules has been renamed to “Az” Module. Microsoft want you to start using this module moving forward. Currently, this new release is on version 0.5.0, and you’ll need to remove the any previous module(s) installed. Information about Azure PowerShell can be found on the following link.

Now, there’s always been a tedious task when manually removing module dependencies, and there’s no exception with the “Az” module.  So, we can all take advantage to PowerShell and create a script to work around this limitation.

And, below is a few options.

My Custom Function

I have realized that sometimes I got duplicate modules installed with different versions installed, and this can create some confusion. My solution was to create a custom PowerShell function “Remove-AzureModule” to remove all existing Azure modules.

function Remove-AzureModule {
[CmdletBinding(SupportsShouldProcess)]
param ()

$Modules = Get-Module -ListAvailable Az* `
| Select-Object -unique Name, Version;

$cnt = 1;
ForEach ($Module in $Modules)
{
if ($PSCmdlet.ShouldProcess($Module.name, 'Uninstall'))
{
Write-Host "[$cnt] - Removing module: $($Module.Name) - Version: $($Module.Version)." -ForegroundColor Yellow;
Uninstall-Module -Name $Module.Name -AllVersions -Force;
$cnt++;
};
};
};

## - Run using the -What If parameter:
Remove-AzureModule -What If

## - Execute to remove Azure modules:
Remove-AzureModule

The purpose of this function to do a full cleanup removing all installed versions of Azure modules from PowerShell.   This function includes a the “-WhatIf” parameter that can be use to run the function without actually performing the “uninstall” process. And, I added a counter for each module when performing the “uninstall” for each of the dependency module.

Then, you can always use the following “Get-Module” command to verify that all modules has been removed.

Get-Module -Listavailable Az* | Select-Object Name, Version

Azure Docs – Uninstall modules with dependencies

Now, while writing this quick blog post, I found in the Microsoft documentation there’s a “Uninstall the Azure PowerShell Module” section. This article provide the “Uninstall-AllModules” function which is a a generic way to remove any previously installed module with dependencies.

Below, is the modified version of the Microsoft function. I added the code to allow the use of the “-What If” parameter, and to do a count of the modules been removed. I think these are a nice to have changes.

## - Uninstall-AllModules from the Microsoft Docs: https://docs.microsoft.com/en-us/powershell/azure/uninstall-azurerm-ps?view=azurermps-6.12.0#uninstall-from-powershell
## - Modified to include the "-WhatIf" parameter and a module counter.

function Uninstall-AllModules
{
[CmdletBinding(SupportsShouldProcess)]
param (
[Parameter(Mandatory = $true)]
[string]
$TargetModule,
[Parameter(Mandatory = $true)]
[string]
$Version,
[switch]
$Force
)

$AllModules = @()

'Creating list of dependencies...'
$target = Find-Module $TargetModule -RequiredVersion $version
$target.Dependencies | ForEach-Object {
$AllModules += New-Object -TypeName psobject -Property @{ name = $_.name; version = $_.minimumVersion }
}
$AllModules += New-Object -TypeName psobject -Property @{ name = $TargetModule; version = $Version }

$cnt = 1;
foreach ($module in $AllModules)
{
Write-Host ("[$cnt] - " + 'Uninstalling {0} version {1}' -f $module.name, $module.version);
$cnt++;
try
{
if ($PSCmdlet.ShouldProcess($module.name, 'Uninstall'))
{
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop;
};
}
catch
{
Write-Host ("`t" + $_.Exception.Message)
}
}
};

## - Example using -WhatIf parameter:
Uninstall-AllModules -TargetModule Az -Version 0.3.0 -Force -WhatIf

## - Example to remove module with dependencies:
Uninstall-AllModules -TargetModule Az -Version 0.3.0 -Force

Unfortunately, this solution will work as long as all dependencies modules has the same version.  So, a little work still need to be done!

Feel free to test the code in both Windows PowerShell and PowerShell Core.

Another way to Manage PowerShell Modules

Take a look at SAPIEN Technologies new product “Module Manager“. You’ll be surprise what you can find installed in your system. This GUI application let you manage modules installed in your system making it easy to update, disable/enable, and install/uninstall modules.

For more Module Manager Preview information check on the following SAPIEN Technologies link.

Recap

As you can see, PowerShell can provide the means to workaround issues you’ll experience in a simple way. At the same time there are both community and Microsoft documentation available that will provide assistance.

Finally, check out third-party products, like the ones from SAPIEN Technologies that are available to increase your productivity in many ways. For more information check on the following SAPIEN Technologies products link.