您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

卸载 Azure PowerShell 模块Uninstall the Azure PowerShell module

本文介绍如何卸载旧版 Azure PowerShell 或将其从系统中完全删除。This article tells you how to uninstall an older version of Azure PowerShell, or completely remove it from your system. 如果你决定彻底卸载 Azure PowerShell,请通过 Send-Feedback cmdlet 为我们提供一些反馈。If you've decided to completely uninstall the Azure PowerShell, give us some feedback through the Send-Feedback cmdlet. 如果你在遇到 Bug 后提出 GitHub 问题,我们将十分感激,会为你解决该问题。If you encountered a bug, we'd appreciate it if you file a GitHub issue so that it can be fixed.

卸载 Az 模块Uninstall the Az module

若要卸载 Az 模块,请使用 Uninstall-Module cmdlet。To uninstall the Az modules, use the Uninstall-Module cmdlet. 但是,Uninstall-Module 只卸载一个模块。However, Uninstall-Module only uninstalls one module. 若要彻底删除 Azure PowerShell,必须单独卸载每个模块。To remove Azure PowerShell completely, you must uninstall each module individually. 如果安装了多个版本的 Azure PowerShell,则卸载过程可能很复杂。Uninstallation can be complicated if you have more than one version of Azure PowerShell installed.

若要检查当前安装了哪些 Azure PowerShell 版本,请运行以下命令:To check which versions of Azure PowerShell you currently have installed, run the following command:

Get-InstalledModule -Name Az -AllVersions
Version             Name                           Repository           Description
-------             ----                           ----------           -----------
0.7.0               Az                             PSGallery            Azure Resource Manager Module
1.0.0               Az                             PSGallery            Azure Resource Manager Module

以下脚本查询 PowerShell 库以获取依赖性子模块的列表。The following script queries the PowerShell Gallery to get a list of dependent submodules. 然后,此脚本会卸载每个子模块的正确版本。Then, the script uninstalls the correct version of each submodule. 需要具有管理员访问权限才能在 ProcessCurrentUser 之外的作用域中运行此脚本。You will need to have administrator access to run this script in a scope other than Process or CurrentUser.

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 会话中。To use this function, copy and paste the code into your PowerShell session. 以下示例演示了如何运行此函数来删除旧版 Azure PowerShell。The following example shows how to run the function to remove an older version of Azure PowerShell.

Uninstall-AllModules -TargetModule Az -Version 0.7.0 -Force

此脚本在运行时会显示每个要卸载的子模块的名称和版本。As the script runs, it will display the name and version of each submodule that is being uninstalled. 若要运行该脚本以仅查看哪一项会被删除,但不删除该项,请使用 -WhatIf 选项。To run the script to only see what would be deleted, without removing it, use the -WhatIf option.

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

备注

如果此脚本无法将确切的依赖项与要卸载的同一版本匹配,它不会卸载该依赖项的_任何_版本。If this script can't match an exact dependency with the same version to uninstall, it won't uninstall any version of that dependecy. 这是因为你的系统上可能有目标模块的其他版本依赖于这些依赖项。This is because there may be other versions of the target module on your system which rely on these dependencies. 在这种情况下会列出依赖项的可用版本。In this case, the available versions of the dependency are listed. 然后,你可以使用 Uninstall-Module 手动删除任何旧版本。You can then remove any old versions manually with Uninstall-Module.

请针对要卸载的每个 Azure PowerShell 版本运行此命令。Run this command for every version of Azure PowerShell that you want to uninstall. 为方便起见,以下脚本将卸载__除了__最新版本之外的所有 Az 版本。For convenience, the following script will uninstall all versions of Az except for the latest.

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

卸载 AzureRM 模块Uninstall the AzureRM module

如果已在系统上安装了 Az 模块并且希望卸载 AzureRM,可以使用两个无需运行以上 Uninstall-AllModules 脚本的选项。If you have the Az module installed on your system and would like to uninstall AzureRM, there are two options that don't require running the Uninstall-AllModules script above. 采用哪种方法取决于你安装 AzureRM 模块的方式。Which method you follow depends on how you installed the AzureRM module. 如果无法确定原始安装方法,请先按 MSI 卸载步骤操作。If you're not sure of your original install method, follow the steps for uninstalling an MSI first.

卸载 Azure PowerShell MSIUninstall Azure PowerShell MSI

如果是使用 MSI 包安装了 Azure PowerShell AzureRM 模块,则必须通过 Windows 系统而不是 PowerShell 进行卸载。If you installed the Azure PowerShell AzureRM modules using the MSI package, you must uninstall through the Windows system rather than PowerShell.

平台Platform 说明Instructions
Windows 10Windows 10 “开始”>“设置”>“应用”Start > Settings > Apps
Windows 7Windows 7
Windows 8Windows 8
“开始”>“控制面板”>“程序”>“卸载程序”Start > Control Panel > Programs > Uninstall a program

转到此屏幕以后,会在程序列表中看到 Azure PowerShellOnce on this screen you should see Azure PowerShell in the program listing. 这是要卸载的应用。This is the app to uninstall. 如果未看到该程序已列出,那么是通过 PowerShellGet 安装的,应按照下一组说明进行操作。If you don't see this program listed, then you installed through PowerShellGet, and should follow the next set of instructions.

从 PowerShell 卸载Uninstall from PowerShell

如果使用 PowerShellGet 安装了 AzureRM,则可以使用作为 Az.Accounts 模块的一部分提供的 Uninstall-AzureRM 命令删除这些模块。If you installed AzureRM with PowerShellGet, then you can remove the modules with the Uninstall-AzureRM command, available as part of the Az.Accounts module. 这将从计算机中删除所有 AzureRM 模块,但需要管理员权限。This removes all AzureRM modules from your machine, but requires administrator privileges.

Uninstall-AzureRm

如果无法成功运行 Uninstall-AzureRM 命令,请使用本文中提供的 Uninstall-AllModules 脚本进行以下调用:If you can't successfully run the Uninstall-AzureRM command, use the Uninstall-AllModules script provided in this article with the following invocation:

$versions = (Get-InstalledModule AzureRM -AllVersions | Select-Object Version)
$versions | foreach { Uninstall-AllModules -TargetModule AzureRM -Version ($_.Version) -Force }