Hey start the uninstallation process by checking the available versions:
Get-InstalledModule -Name Az -AllVersions
Output:
Version Name Repository Description
------- ---- ---------- -----------
0.7.0 Az PSGallery Azure Resource Manager Module
1.0.0 Az PSGallery Azure Resource Manager Module
Get the list of dependent submodules:
function Uninstall-AllModules {
param(
[Parameter(Mandatory=$true)]
[string]$TargetModule,
[Parameter(Mandatory=$true)]
[string]$Version,
[switch]$Force,
[switch]$WhatIf
)
$AllModules = @()
'Creating list of dependencies...'
$target = Find-Module $TargetModule -RequiredVersion $version
$target.Dependencies | ForEach-Object {
if ($_.PSObject.Properties.Name -contains 'requiredVersion') {
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$_.requiredVersion}
}
else { # Assume minimum version
# Minimum version actually reports the installed dependency
# which is used, not the actual "minimum dependency." Check to
# see if the requested version was installed as a dependency earlier.
$candidate = Get-InstalledModule $_.name -RequiredVersion $version -ErrorAction Ignore
if ($candidate) {
$AllModules += New-Object -TypeName psobject -Property @{name=$_.name; version=$version}
}
else {
$availableModules = Get-InstalledModule $_.name -AllVersions
Write-Warning ("Could not find uninstall candidate for {0}:{1} - module may require manual uninstall. Available versions are: {2}" -f $_.name,$version,($availableModules.Version -join ', '))
}
}
}
$AllModules += New-Object -TypeName psobject -Property @{name=$TargetModule; version=$Version}
foreach ($module in $AllModules) {
Write-Host ('Uninstalling {0} version {1}...' -f $module.name,$module.version)
try {
Uninstall-Module -Name $module.name -RequiredVersion $module.version -Force:$Force -ErrorAction Stop -WhatIf:$WhatIf
} catch {
Write-Host ("`t" + $_.Exception.Message)
}
}
}
Remove older versions of powershell:
Uninstall-AllModules -TargetModule Az -Version 0.7.0 -Force
Output:
Creating list of dependencies...
Uninstalling Az.Profile version 0.7.0
Uninstalling Az.Aks version 0.7.0
Uninstalling Az.AnalysisServices version 0.7.0
...
Command to uninstall:
$versions = (Get-InstalledModule Az -AllVersions | Select-Object Version)
$versions[0..($versions.Length-2)] | foreach { Uninstall-AllModules -TargetModule Az -Version ($_.Version) -Force }