Install Microsoft Entra PowerShell

Microsoft Entra PowerShell 模組讓 IT 專業人員能有效管理 Microsoft Entra 資源,確保跨平台的無縫整合與功能。 此模組提升您自動化與簡化行政工作的能力。

在本文中,您將學習如何在不同作業系統上安裝、驗證、更新及排除 Microsoft Entra PowerShell 模組。

Microsoft Entra PowerShell 模組分為兩個模組,每個模組包含可獨立安裝的子模組。 兩個主要模組為:

  • Microsoft.Entra - Microsoft Entra PowerShell 的正式推出v1.0版本。 它指向 Microsoft Graph v1.0 和 Microsoft Graph PowerShell SDK v1.0 資源。
  • Microsoft。Entra.Beta - Beta Microsoft Entra PowerShell 的預覽版。 它指向 Microsoft Graph Beta 和 Microsoft Graph PowerShell SDK Beta 資源。

建議在所有平台(包括 Windows、Linux 和 macOS)上搭配 Microsoft Entra PowerShell 模組使用 PowerShell 7 或更新版本

在 Windows 上安裝 Microsoft Entra PowerShell 模組

模組推薦的安裝方法及 PowerShell 版本:

Windows 上的先決條件

  • 從 PowerShell 執行下列命令,以判斷您的 PowerShell 版本:

    $PSVersionTable.PSVersion
    
  • 確認你是否安裝了模組:

    Get-Module -Name Microsoft.Entra -ListAvailable
    
  • 將 PowerShell 執行原則設定為遠端簽署或限制較少

    • 檢查 PowerShell 執行原則:

      Get-ExecutionPolicy -List
      
    • 將 PowerShell 執行原則設定為遠端簽署:

      Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
      

    如需執行原則的詳細資訊,請參閱 about_Execution_Policies

Installation

安裝模組時,你可以選擇安裝整個模組或特定的子模組。 以下範例說明如何為 v1.0Beta 安裝整個模組。

使用 Install-Module 指令來安裝模組。

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope CurrentUser -Force -AllowClobber

你也可以選擇性地使用 Scope 參數來更改安裝的範圍。 此操作需要管理員權限。

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope AllUsers -Force -AllowClobber

安裝特定的子模組

安裝特定模組非常適合自動化情境,例如使用 Azure Functions 和 Azure 自動化。

執行以下指令,在 PowerShell 資源庫 中列出所有 Microsoft.Entra GA/v1.0 模組:

Find-Module -Name "Microsoft.Entra*" -Repository PSGallery |
Where-Object { $_.Name -notmatch "beta" }

此指令列出 PowerShell 資源庫 中所有 Microsoft Entra PowerShell GA/v1.0 模組。

例如,若要安裝 Users 模組,請執行下列指令:

Install-Module -Name Microsoft.Entra.Users -Repository PSGallery -Force -AllowClobber

在 Linux 安裝 Microsoft Entra PowerShell

Linux 上的先決條件

在 Linux 上安裝

安裝模組時,你可以選擇安裝整個模組或特定的子模組。 以下範例展示了如何安裝整個模組。

開啟終端機或其他 shell 環境應用程式,然後執行 pwsh 以啟動 PowerShell。

使用 Install-Module 指令碼來安裝模組:

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope CurrentUser -Force -AllowClobber

你也可以選擇性地使用 Scope 參數來更改安裝的範圍。 此操作需要管理員權限。

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope AllUsers -Force -AllowClobber

在 Linux 上安裝特定的子模組

安裝特定模組非常適合自動化情境,例如使用 Azure Functions 和 Azure 自動化。

執行以下指令,在 PowerShell 資源庫 中列出所有 Microsoft.Entra GA/v1.0 模組:

Find-Module -Name "Microsoft.Entra*" -Repository PSGallery |
Where-Object { $_.Name -notmatch "beta" }

此指令列出 PowerShell 資源庫 中所有 Microsoft Entra PowerShell GA/v1.0 模組。

例如,若要安裝 Users 模組,請執行以下指令:

Install-Module -Name Microsoft.Entra.Users -Repository PSGallery -Force -AllowClobber

Install Microsoft Entra PowerShell on macOS

本文說明如何在 macOS 的 PowerShell 資源庫 安裝模組。

macOS 上的先決條件

macOS 上的安裝

安裝模組時,你可以選擇安裝整個模組或特定的子模組。 以下範例展示了如何安裝整個模組。

開啟終端機或其他 shell 環境應用程式,然後執行 pwsh 以啟動 PowerShell。

使用 Install-Module cmdlet 來安裝 Microsoft Entra PowerShell 模組:

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope CurrentUser -Force -AllowClobber

你也可以選擇性地使用 Scope 參數來更改安裝的範圍。 此操作需要管理員權限。

Install-Module -Name Microsoft.Entra -Repository PSGallery -Scope AllUsers -Force -AllowClobber

在 macOS 上安裝特定的子模組

安裝特定模組非常適合自動化情境,例如使用 Azure Functions 和 Azure 自動化。

執行以下指令,在 PowerShell 資源庫 中列出所有 Microsoft.Entra GA/v1.0 模組:

Find-Module -Name "Microsoft.Entra*" -Repository PSGallery |
Where-Object { $_.Name -notmatch "beta" }

此指令列出 PowerShell 資源庫 中所有 Microsoft Entra PowerShell GA/v1.0 模組。

例如,若要安裝 Users 模組,請執行下列指令:

Install-Module -Name Microsoft.Entra.Users -Repository PSGallery -Force -AllowClobber

確認已安裝模組版本與位置

安裝完成後,你可以用以下指令驗證已安裝的子模組、版本和位置。

Get-InstalledModule -Name Microsoft.Entra* |
Where-Object { $_.Name -notmatch "Beta" } | Format-Table Name, Version, InstalledLocation -AutoSize

此指令顯示已安裝的 Microsoft Entra PowerShell 模組、版本及安裝位置。 輸出版本應該與 PowerShell 資源庫 上的最新版本相符。 你現在可以使用這個模組了。

Name                                Version InstalledLocation
----                                ------- -----------------
Microsoft.Entra                     1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra\1.0.1
Microsoft.Entra.Applications        1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Applications\1.0.1
Microsoft.Entra.Authentication      1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Authentication\1.0.1
Microsoft.Entra.DirectoryManagement 1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.DirectoryManagement\1.0.1
Microsoft.Entra.Governance          1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Governance\1.0.1
Microsoft.Entra.Groups              1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Groups\1.0.1
Microsoft.Entra.Reports             1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Reports\1.0.1
Microsoft.Entra.SignIns             1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.SignIns\1.0.1
Microsoft.Entra.Users               1.0.1   <PowerShell-Path>\PowerShell\Modules\Microsoft.Entra.Users\1.0.1

例如,要確認是否安裝了特定子模組 Users ,請執行:

Get-InstalledModule -Name Microsoft.Entra.Users

輸出版本應該與 PowerShell 資源庫 上的最新版本相符。

Version              Name                                Repository           Description
-------              ----                                ----------           -----------
1.0.1                Microsoft.Entra.Users               PSGallery            Microsoft Entra PowerShell

針對安裝問題進行疑難解答

錯誤 原因 解決方法
安裝模組:找不到與參數名稱 AllowPrerelease 相符的參數。 你用的是舊版的 Install-Module。 要升級,請參考本指南。 這個問題只發生在 Windows 平台。
依賴模組「module-name」並未安裝在這台電腦上。 若要使用目前的模組「Microsoft.Entra」,請確認其相依模組「module-name」已安裝。 Microsoft Entra PowerShell 相依性未安裝 安裝時請使用此 腳本
指令小程式已經存在於系統中 新增 -AllowClobber 參數: Install-Module -Name Microsoft.Entra -Repository PSGallery -Force -AllowClobber
此系統已提供以下指令:『Enable-EntraAzureADAlias、Get-EntraUnsupportedCommand、Test-EntraScript』 Betav1.0 任一者已安裝時,就會發生衝突 要解決問題,請 卸載 該模組版本。

關於其他常見安裝及一般問題的解決方案,請參見「 故障排除模組安裝問題」。

登入

要開始使用 Microsoft Entra PowerShell 模組管理您的 Microsoft Entra 資源,請啟動 PowerShell 會話並執行 Connect-Entra 登入 Microsoft Entra ID:

Connect-Entra -Scopes 'User.Read.All'
Get-EntraUser -Filter "userPrincipalName eq 'SawyerM@contoso.com'"

使用您的 Microsoft Entra 憑證登入所開啟的登入視窗。

您必須針對啟動的每個新 PowerShell 會話重複此步驟。

欲了解更多其他認證情境資訊,請參見 更多認證情境

更新模組

如果模組已安裝,請依照以下步驟更新。

首先,請依照本文 「驗證安裝 」部分的指引,確認模組的版本與位置。

更新為最新版本

使用 Update-Module 來更新到 Microsoft Entra PowerShell 模組的最新版本。

Update-Module -Name Microsoft.Entra -Force

要更新特定的子模組,例如 Users,執行:

Update-Module -Name Microsoft.Entra.Users -Force

使用 Update-Module 更新 Microsoft Entra PowerShell 模組,並不會從您的系統中移除該模組的舊版。

解除安裝模組

要移除模組,請執行以下指令:

Uninstall-Module -Name Microsoft.Entra -AllVersions -Force

要卸載特定子模組,例如 Users,執行:

Uninstall-Module -Name Microsoft.Entra.Users -AllVersions -Force

要解除所有子模組,請執行:

$InstalledSubModules = Get-InstalledModule -Name Microsoft.Entra.* |
Where-Object { $_.Name -notmatch "Beta" }
foreach ($module in $InstalledSubModules) {
    Write-Host "Checking for submodule: $($module.Name)" -ForegroundColor Yellow -BackgroundColor DarkBlue
    Uninstall-Module -Name $module.Name -Force -ErrorAction Stop
    Write-Host "Successfully uninstalled submodule: $($module.Name)" -ForegroundColor Green
}

下一步

  • 想了解更多關於如何使用 Microsoft Entra PowerShell 模組,請參閱「導航模組」文章。