你当前正在访问 Microsoft Azure Global Edition 技术文档网站。 如果需要访问由世纪互联运营的 Microsoft Azure 中国技术文档网站,请访问 https://docs.azure.cn

卸载 Azure PowerShell 模块

警告

AzureRM PowerShell 模块已自 2024 年 2 月 29 日起正式弃用。 为了确保持续获得支持和更新,建议用户从 AzureRM 迁移到 Az PowerShell 模块。

尽管 AzureRM 模块仍可运行,但不再受到维护或支持,任何继续使用的行为都由用户自行决定并自行承担风险。 有关过渡到 Az 模块的指导,请参阅我们的迁移资源

本文介绍如何卸载旧版 Azure PowerShell 或将其从系统中完全删除。 如果你决定彻底卸载 Azure PowerShell,请通过 Send-Feedback cmdlet 为我们提供一些反馈。 如果你在遇到 bug 后提出 GitHub 问题,我们将十分感激。

卸载 Azure PowerShell MSI

若是使用 MSI 包安装的 Azure PowerShell,则必须通过 Windows 系统而不是 PowerShell 进行卸载。

平台 说明
Windows 10 “开始”>“设置”>“应用”
Windows 7
Windows 8
“开始”>“控制面板”>“程序”>“卸载程序”

转到此屏幕以后,会在程序列表中看到 Azure PowerShell。 这是要卸载的应用。

从 PowerShell 卸载

若是使用 PowerShellGet 安装的 Azure PowerShell,则可使用 Uninstall-Module cmdlet。 但是,Uninstall-Module 只卸载一个模块。 若要彻底删除 Azure PowerShell,必须单独卸载每个模块。 如果安装了多个版本的 Azure PowerShell,则卸载过程可能很复杂。

若要检查当前安装了哪些 Azure PowerShell 版本,请运行以下命令:

Get-InstalledModule -Name AzureRM -AllVersions
Version              Name                                Repository           Description
-------              ----                                ----------           -----------
6.11.0               AzureRM                             PSGallery            Azure Resource Manager Module
6.13.1               AzureRM                             PSGallery            Azure Resource Manager Module

以下脚本查询 PowerShell 库以获取依赖性子模块的列表。 然后,此脚本会卸载每个子模块的正确版本。 需要具有管理员访问权限才能在 ProcessCurrentUser 之外的作用域中运行此脚本。

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

若要使用此函数,请将代码复制并粘贴到 PowerShell 会话中。 以下示例演示了如何运行此函数来删除旧版 Azure PowerShell。

Uninstall-AllModules -TargetModule AzureRM -Version 4.4.1 -Force

此脚本在运行时会显示每个要卸载的子模块的名称和版本。 若要运行该脚本以仅查看哪一项会被删除,但不删除该项,请使用 -WhatIf 选项。

Creating list of dependencies...
Uninstalling AzureRM.Profile version 3.4.1
Uninstalling Azure.Storage version 3.4.1
Uninstalling AzureRM.AnalysisServices version 0.4.7
Uninstalling Azure.AnalysisServices version 0.4.7
...

注意

如果此脚本无法将确切的依赖项与要卸载的同一版本匹配,它不会卸载该依赖项的任何版本。 这是因为你的系统上可能有目标模块的其他版本依赖于这些依赖项。 在这种情况下会列出依赖项的可用版本。 然后,你可以使用 Uninstall-Module 手动删除任何旧版本。

请针对要卸载的每个 Azure PowerShell 版本运行此命令。 为方便起见,以下脚本将会卸载 AzureRM 最新版本之外的所有 AzureRM 版本。

$versions = (Get-InstalledModule -Name AzureRM -AllVersions | Select-Object -Property Version)
$versions[0..($versions.Length-2)]  | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }