Bobcares

How to Uninstall Azure ad PowerShell Module

by | Oct 4, 2020

Are you trying to uninstall the Azure ad PowerShell module? We can help you with it.

Here at Bobcares, we have seen several such PowerShell related queries as part of our Server Management Services for web hosts and online service providers.

Today, we’ll take a look at how to uninstall the Azure ad Powershell module.

 

How we uninstall azure ad PowerShell module

Now let’s see how our Support Engineers uninstall the Azure ad PowerShell module.

 

Uninstall the Az PowerShell module from MSI

If you have installed the Az PowerShell module using the MSI package then you would need to uninstall it through the Windows System.

In Windows 10, you can uninstall it by clicking on Start >> Apps.

In Windows 7,8, you can uninstall it by Clicking on Start >> Control Panel >> Programs >> Uninstall a program.

Once you have reached this screen then you will see the Azure PowerShell in the program listing. You would need to uninstall this app.

 

Uninstall the Az PowerShell module from PowerShellGet

In order to uninstall the Az modules, we can use the Uninstall-Module cmdlet. However, the Uninstall-Module will only uninstall a single module. So in order to remove the Az PowerShell module completely then you would need to uninstall all the modules individually.

Run the below command to check the version of the Az PowerShell module

Get-InstalledModule -Name Az -AllVersions

The below script will uninstall the correct version of each submodule. However, to run this script you need administrator access.

function Uninstall-AzModule {
[CmdletBinding(SupportsShouldProcess)]
param(
[ValidateNotNullOrEmpty()]
[ValidateSet('Az','AzureRM','Azure')]
[string]$Name = 'Az',

[Parameter(Mandatory)]
[string]$Version,

[switch]$AllowPrerelease
)

$Params = @{}

if ($PSBoundParameters.AllowPrerelease) {
$Params.AllowPrerelease = $true
}

$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] 'Administrator')

if (-not(Get-InstalledModule -Name $Name -RequiredVersion $Version -ErrorAction SilentlyContinue -OutVariable RootModule @Params)) {

Write-Warning -Message "Uninstall aborted. $Name version $Version not found."

} elseif (($RootModule.InstalledLocation -notlike "*$env:USERPROFILE*") -and ($IsAdmin -eq $false)) {

Write-Warning -Message "Uninstall aborted. $Name version $Version exists in a system path. PowerShell must be run elevated as an admin to remove it."

} elseif ((Get-Process -Name powershell, pwsh -OutVariable Sessions -ErrorAction SilentlyContinue).Count -gt 1) {

Write-Warning -Message "Uninstall aborted. Please close all other PowerShell sessions before continuing. There are currently $($Sessions.Count) PowerShell sessions running."

} else {
Write-Verbose -Message 'Creating list of dependencies...'
$target = Find-Module -Name $Name -RequiredVersion $Version @Params

$AllModules = @([pscustomobject]@{
Name = $Name
Version = $Version
})

$AllModules += foreach ($dependency in $target.Dependencies) {
switch ($dependency.keys) {
{$_ -contains 'RequiredVersion'} {$UninstallVersion = $dependency.RequiredVersion; break}
{$_ -contains 'MinimumVersion'} {$UninstallVersion = $dependency.MinimumVersion; break}
}

[pscustomobject]@{
Name = $dependency.Name
Version = $UninstallVersion
}
}

[int]$i = 100 / $AllModules.Count

foreach ($module in $AllModules) {
Write-Progress -Activity 'Uninstallation in Progress' -Status "$i% Complete:" -PercentComplete $i
$i++

if (Get-InstalledModule -Name $module.Name -RequiredVersion $module.Version -ErrorAction SilentlyContinue @Params) {
Write-Verbose -Message "Uninstalling $($module.Name) version $($module.Version)"

Remove-Module -FullyQualifiedName @{ModuleName=$module.Name;ModuleVersion=$module.Version} -ErrorAction SilentlyContinue

try {
if ($PSCmdlet.ShouldProcess("$($module.Name) version $($module.Version)")) {
Uninstall-Module -Name $module.Name -RequiredVersion $module.Version -Force -ErrorAction Stop @Params
}
$State = 'Uninstalled'
} Catch {
$State = 'Manual uninstall required'
Write-Verbose -Message "$($module.Name) version: $($module.Version) may require manual uninstallation."
}

} else {
$State = 'Not found'
Write-Verbose -Message "$($module.Name) version: $($module.Version) not found."
}

if (-not $PSBoundParameters.WhatIf) {
[pscustomobject]@{
ModuleName = $module.Name
Version = $module.Version
State = $State
}
}

}
}
}

Copy and paste the above script in your PowerShell session. The below example shows to remove an older version of the Az PowerShell module and its submodules.

Uninstall-AzModule -Name Az -Version 1.8.0

As the script runs, it displays the Name, Version, and State of each submodule that is being uninstalled.

 

Uninstall the AzureRM module

In case, if you have installed the Az module on your system and want to uninstall the AzureRM, then there are two ways to uninstall it. It completely depends on how you have installed it.

 

Uninstall the AzureRM PowerShell module from MSI

You can follow this method if you have installed the AzureRM PowerShell module using the MSI package. Here you would need to uninstall it through the Windows system.

In Windows 10, you can uninstall it by clicking on Start >> Settings >> Apps.

In Windows 7,8, you can uninstall it by Clicking on Start >> Control Panel >> Programs >> Uninstall a program.

Once you have reached this screen then you will see the Azure PowerShell or Microsoft Azure PowerShell – Month Year in the program listing. You would need to uninstall this app.

 

Uninstall the AzureRM PowerShell module from PowerShellGet

You can follow this method if you have installed the AzureRM PowerShell module using the PowerShellGet.

You can use the Uninstall-AzureRM cmdlet for uninstalling purposes.

Run the below command to remove all AzureRM modules from your machine. However, to run it you need to have the administrator privileges.

Uninstall-AzureRm

[Need any further assistance with PowerShell queries? – We are here to help you.]

 

Conclusion

Today, we saw how our Support Engineers uninstall the Az PowerShell module and AzureRM module from MSI and PowerShellGet.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";

0 Comments

Submit a Comment

Your email address will not be published. Required fields are marked *

Never again lose customers to poor
server speed! Let us help you.