卸载 Microsoft Graph PowerShell 模块

Jacki

Microsoft Graph PowerShell 模块是管理任务的一个非常重要的模块。但是,有时更新模块会出现问题,或者可能无法按预期工作。因此,您想完全删除它并开始全新安装。在本文中,您将了解如何从系统中卸载 Microsoft Graph PowerShell 模块。

Microsoft Graph PowerShell 模块(Microsoft.Graph、Microsoft.Graph.Beta)用于管理 Microsoft Entra 和 Microsoft 365。但是,由于模块膨胀、版本冲突、切换到不同的工具或只是为了清理,您可能需要完全删除它们。

推荐阅读:如何安装 Microsoft PowerShell PSResourceGet 模块

本指南涵盖了以下方面的完全删除:

  • Windows PowerShell 5 和 PowerShell 7
  • 当前用户和所有用户
  • 通过 PowerShellGet 和 PSResourceGet 安装的模块

重要的:在 Windows PowerShell 和 PowerShell 中运行命令。当然,前提是您的系统上安装了 PowerShell 7.x。

步骤 1. 获取已安装的 Microsoft Graph 模块

获取 Microsoft Graph PowerShell 模块的列表。在 Windows PowerShell 5 和 PowerShell 7 中运行所有命令。

Get-InstalledModule Microsoft.Graph* -ErrorAction SilentlyContinue
Get-InstalledPSResource Microsoft.Graph* -Scope CurrentUser -ErrorAction SilentlyContinue
Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers -ErrorAction SilentlyContinue

步骤 2. 卸载 Microsoft Graph PowerShell 模块

卸载当前用户和所有用户的 Microsoft Graph PowerShell 模块。在 Windows PowerShell 5 和 PowerShell 7 中运行所有命令。

Get-InstalledModule Microsoft.Graph* | Uninstall-Module -AllVersions -Force
Get-InstalledPSResource Microsoft.Graph* -Scope CurrentUser | Uninstall-PSResource -Scope CurrentUser -SkipDependencyCheck
Get-InstalledPSResource Microsoft.Graph* -Scope AllUsers | Uninstall-PSResource -Scope AllUsers -SkipDependencyCheck

步骤 3. 手动删除 Microsoft Graph PowerShell 模块文件夹

为了确保所有内容都被完全删除,您可以以管理员身份打开 Windows PowerShell 或 PowerShell 7 并运行脚本。

笔记:如果您的 Windows 操作系统不是英语,请更改脚本中的文件夹名称。

# Get all module directories for both Windows PowerShell and PowerShell 7
$paths = @(
    "$env:ProgramFilesWindowsPowerShellModules", # Windows PowerShell (all users)
    "$env:USERPROFILEDocumentsWindowsPowerShellModules", # Windows PowerShell (current user)
    "$env:ProgramFilesPowerShellModules", # PowerShell 7.x (all users)
    "$env:USERPROFILEDocumentsPowerShellModules" # PowerShell 7.x (current user)
)

# Go through each module directory path
foreach ($path in $paths) {
    Get-ChildItem -Path $path -Filter "Microsoft.Graph*" -Directory -ErrorAction SilentlyContinue |
    Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
}

现在一切都已完全从系统中删除。您可以安装 Microsoft Graph PowerShell 模块。

就是这样!

结论

您了解了如何卸载 Microsoft Graph PowerShell 模块。删除 Windows PowerShell 5 和 PowerShell 7 中的 Microsoft Graph PowerShell 模块,确保您不会错过任何步骤。这样,您就可以确定它已从系统中的所有位置删除。

您喜欢这篇文章吗?您可能还喜欢使用 PowerShell 在 Microsoft 365 中强制注销用户。不要忘记关注我们并分享这篇文章。