使用 PowerShell 連線至 Microsoft 365Connect to Microsoft 365 with PowerShell

本文適用於 Microsoft 365 企業版和 Office 365 企業版。This article applies to both Microsoft 365 Enterprise and Office 365 Enterprise.

適用於 Microsoft 365 的 PowerShell 允許您從命令列管理您的 Microsoft 365 設定。PowerShell for Microsoft 365 enables you to manage your Microsoft 365 settings from the command line. 要連線至 PowerShell,只需安裝所需的軟體,然後連線至 Microsoft 365 組織。To connect to PowerShell, just install the required software and then connect to your Microsoft 365 organization.

可以用來連線至 Microsoft 365 及管理使用者帳戶、群組和授權的 PowerShell 模組有兩個版本:There are two versions of the PowerShell module that you can use to connect to Microsoft 365 and administer user accounts, groups, and licenses:

  • 適用於圖表的 Azure Active Directory PowerShell,其 cmdlet 名稱中包含 AzureADAzure Active Directory PowerShell for Graph, whose cmdlets include AzureAD in their name
  • 適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組,其 cmdlets 名稱中包含 MSolMicrosoft Azure Active Directory Module for Windows PowerShell, whose cmdlets include Msol in their name

目前,適用於圖表的 Azure Active Directory PowerShell 模組無法完全取代適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組中針對使用者、群組和授權管理的功能。Currently, the Azure Active Directory PowerShell for Graph module doesn't completely replace the functionality of the Microsoft Azure Active Directory Module for Windows PowerShell module for user, group, and license administration. 在某些情況下,您需要同時使用這兩個版本。In some cases, you need to use both versions. 您可以在同一部電腦上安全地安裝這兩個版本。You can safely install both versions on the same computer.

開始之前有哪些須知?What do you need to know before you begin?

作業系統Operating system

必須使用 64 位元的 Windows 版本。You must use a 64-bit version of Windows. 2014 年終止對 32 位元版本的適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組的支援。Support for the 32-bit version of the Microsoft Azure Active Directory Module for Windows PowerShell ended in 2014.

您可以使用下列 Windows 版本:You can use the following versions of Windows:

  • Windows 10、Windows 8.1、Windows 8 或 Windows 7 Service Pack 1 (SP1)Windows 10, Windows 8.1, Windows 8, or Windows 7 Service Pack 1 (SP1)

  • Windows Server 2019、Windows Server 2016、Windows Server 2012 R2、Windows Server 2012 或 Windows Server 2008 R2 SP1Windows Server 2019, Windows Server 2016, Windows Server 2012 R2, Windows Server 2012, or Windows Server 2008 R2 SP1

注意

若為 Windows 8.1、Windows 8、Windows 7 Service Pack 1 (SP1)、Windows Server 2012 R2、Windows Server 2012 和 Windows Server 2008 R2 SP1,請下載並安裝 Windows Management Framework 5.1.For Windows 8.1, Windows 8, Windows 7 Service Pack 1 (SP1), Windows Server 2012 R2, Windows Server 2012, and Windows Server 2008 R2 SP1, download and install the Windows Management Framework 5.1.

PowerShellPowerShell

  • 針對 Azure Active Directory PowerShell 的圖表模組,請務必使用 PowerShell 版本 5.1 或更新版本。For the Azure Active Directory PowerShell for Graph module, you must use PowerShell version 5.1 or later.

  • 針對適用於 Windows PowerShell 模組的 Microsoft Azure Active Directory 模組,請務必使用 PowerShell 版本 5.1 或更新版本 (最多可至 PowerShell 版本 6)。For the Microsoft Azure Active Directory Module for Windows PowerShell module, you must use PowerShell version 5.1 or later, up to PowerShell version 6. 無法使用 PowerShell 版本 7。You can't use PowerShell version 7.

注意

這些程序適用於屬於 Microsoft 365 系統管理員角色成員的使用者。These procedures are intended for users who are members of a Microsoft 365 admin role. 如需詳細資訊,請參閱關於系統管理員角色For more information, see About admin roles.

與 Azure Active Directory PowerShell for Graph 模組連線Connect with the Azure Active Directory PowerShell for Graph module

在Azure Active Directory PowerShell 圖表模組中的命令,其 Cmdlet 名稱中會包含 AzureADCommands in the Azure Active Directory PowerShell for Graph module have AzureAD in their cmdlet name. 您可以安裝 Azure Active Directory PowerShell 的圖表Azure PowerShellYou can install the Azure Active Directory PowerShell for Graph module or Azure PowerShell.

針對需要 Azure Active Directory PowerShell for Graph 模組中新 Cmdlet 的程序,請按照下列步驟來安裝模組,並連線至您的 Microsoft 365 訂閱。For procedures that require the new cmdlets in the Azure Active Directory PowerShell for Graph module, follow these steps to install the module and connect to your Microsoft 365 subscription.

注意

如需支援不同版本的 Microsoft Windows 的詳細資訊,請參閱 Azure Active Directory PowerShell for Graph 模組For information about support for different versions of Windows, see Azure Active Directory PowerShell for Graph module .

步驟 1:安裝必要的軟體Step 1: Install the required software

這些步驟只需在電腦上執行一次。These steps are required only one time on your computer. 但您可能需要定期更新軟體。But you'll likely need to update the software periodically.

  1. 開啟提升權限的 Windows PowerShell 命令提示字元視窗 (以系統管理員身分執行 Windows PowerShell)。Open an elevated Windows PowerShell Command Prompt window (run Windows PowerShell as an administrator).

  2. 執行此命令︰Run this command:

    Install-Module -Name AzureAD
    

根據預設,PowerShell 資源庫 (PSGallery) 未針對 PowerShellGet 設定為受信賴的存放庫。By default, the PowerShell Gallery (PSGallery) isn't configured as a trusted repository for PowerShellGet. 第一次使用 PSGallery 時,您會看到下列訊息:The first time you use the PSGallery, you'll see the following message:

Untrusted repository

You are installing the modules from an untrusted repository. If you trust this repository, change
its InstallationPolicy value by running the `Set-PSRepository` cmdlet.

Are you sure you want to install the modules from 'PSGallery'?
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "N"):

回答 [是] 或 [全部皆是] 以繼續安裝。Answer Yes or Yes to All to continue with the installation.

步驟 2:連接到您的 Microsoft 365 訂閱的 Azure ADStep 2: Connect to Azure AD for your Microsoft 365 subscription

若要使用帳戶名稱和密碼或使用多重要素驗證連接到您的 Microsoft 365 訂閱的 Azure Active Directory (Azure AD),請從 Windows PowerShell 命令提示字元執行下列其中一個命令。To connect to Azure Active Directory (Azure AD) for your Microsoft 365 subscription with an account name and password or with multi-factor authentication, run one of these commands from a Windows PowerShell command prompt. (不需提升權限)(It doesn't have to be elevated.)

Office 365 雲端Office 365 cloud 命令Command
Office 365 全球 (+GCC)Office 365 Worldwide (+GCC) Connect-AzureAD
21 Vianet 運作的 Office 365Office 365 operated by 21 Vianet Connect-AzureAD -AzureEnvironmentName AzureChinaCloud
Office 365 GermanyOffice 365 Germany Connect-AzureAD -AzureEnvironmentName AzureGermanyCloud
Office 365 美國政府 DoD 和 Office 365 美國政府 GCC HighOffice 365 U.S. Government DoD and Office 365 U.S. Government GCC High Connect-AzureAD -AzureEnvironmentName AzureUSGovernment

[登入您的帳戶] 對話方塊中,輸入您的 Microsoft 365 公司或學校帳戶使用者名稱和密碼,然後選取 [確定]In the Sign into your account dialog box, type your Microsoft 365 work or school account user name and password, and then select OK.

如果使用多重要素驗證,請按照說明提供其他身份驗證資訊,例如驗證碼。If you're using multi-factor authentication, follow the instructions to provide additional authentication information, such as a verification code.

連線之後,您可以對 Azure Active Directory PowerShell for Graph 模組使用這些 Cmdlet。After you connect, you can use the cmdlets for the Azure Active Directory PowerShell for Graph module.

與適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組連線Connect with the Microsoft Azure Active Directory Module for Windows PowerShell

注意

在適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組中的 Cmdlet 名稱會包含 MsolCmdlets in the Microsoft Azure Active Directory Module for Windows PowerShell have Msol in their name.

PowerShell 版本 7 和更新版本不支援適用於 Windows PowerShell 模組的 Microsoft Azure Active Directory 模組和名稱有 Msol 的 Cmdlet。PowerShell version 7 and later don't support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. 針對 PowerShell 版本 7 和更高版本,請務必使用 Azure Active Directory PowerShell 的圖表模組或 Azure PowerShell。For PowerShell version 7 and later, you must use the Azure Active Directory PowerShell for Graph module or Azure PowerShell.

PowerShell Core 不支援適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組和名稱有 Msol 的 Cmdlet。PowerShell Core doesn't support the Microsoft Azure Active Directory Module for Windows PowerShell module and cmdlets with Msol in their name. 從 PowerShell 執行這些 Cmdlet。Run these cmdlets from Windows PowerShell.

步驟 1:安裝必要的軟體Step 1: Install the required software

這些步驟只需在電腦上執行一次。These steps are required only one time on your computer. 但您可能需要定期更新軟體。But you'll likely need to update the software periodically.

  1. 如果您未執行 Windows 10,請安裝 64 位元版本的 Microsoft Online Services 登入小幫手:適用於 IT 專業人員的 Microsoft Online Services 登入小幫手 RTWIf you're not running Windows 10, install the 64-bit version of the Microsoft Online Services Sign-in Assistant: Microsoft Online Services Sign-in Assistant for IT Professionals RTW.

  2. 請按照以下步驟,安裝適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組:Follow these steps to install the Microsoft Azure Active Directory Module for Windows PowerShell:

    1. 開啟提升權限的 Windows PowerShell 命令提示字元 (以系統管理員身分執行 Windows PowerShell)。Open an elevated Windows PowerShell command prompt (run Windows PowerShell as an administrator).
    2. 執行 Install-Module MSOnline 命令。Run the Install-Module MSOnline command.
    3. 如果系統提示您安裝 NuGet 提供者,請輸入 Y,然後按 Enter 鍵。If you're prompted to install the NuGet provider, type Y and press Enter.
    4. 如果系統提示您從 PSGallery 安裝模組,請輸入 Y,然後按 Enter 鍵。If you're prompted to install the module from PSGallery, type Y and press Enter.

步驟 2:連接到您的 Microsoft 365 訂閱的 Azure ADStep 2: Connect to Azure AD for your Microsoft 365 subscription

若要使用帳戶名稱和密碼或使用多重要素驗證連接到您的 Microsoft 365 訂閱的 Azure AD,請從 Windows PowerShell 命令提示字元執行下列其中一個命令。To connect to Azure AD for your Microsoft 365 subscription with an account name and password or with multi-factor authentication, run one of these commands from a Windows PowerShell command prompt. (不需提升權限)(It doesn't have to be elevated.)

Office 365 雲端Office 365 cloud 命令Command
Office 365 全球 (+GCC)Office 365 Worldwide (+GCC) Connect-MsolService
21 Vianet 運作的 Office 365Office 365 operated by 21 Vianet Connect-MsolService -AzureEnvironment AzureChinaCloud
Office 365 GermanyOffice 365 Germany Connect-MsolService -AzureEnvironment AzureGermanyCloud
Office 365 美國政府 DoD 和 Office 365 美國政府 GCC HighOffice 365 U.S. Government DoD and Office 365 U.S. Government GCC High Connect-MsolService -AzureEnvironment USGovernment

[登入您的帳戶] 對話方塊中,輸入您的 Microsoft 365 公司或學校帳戶使用者名稱和密碼,然後選取 [確定]In the Sign into your account dialog box, type your Microsoft 365 work or school account user name and password, and then select OK.

如果使用多重要素驗證,請按照說明提供其他身份驗證資訊,例如驗證碼。If you're using multi-factor authentication, follow the instructions to provide additional authentication information, such as a verification code.

如何得知它的運作正常?How do you know it worked?

如果您沒有收到錯誤訊息,則已成功連線。If you don't get an error message, you connected successfully. 若要快速測試,請執行 Microsoft 365 cmdlet,例如 Get-MsolUser,然後查看結果。For quick test, run a Microsoft 365 cmdlet, such as Get-MsolUser, and see the results.

如果收到錯誤訊息,請檢查下列問題:If you get an error message, check the following issues:

  • 密碼錯誤是常見的問題A common problem is an incorrect password. 再次執行步驟 2,並密切注意您輸入的使用者名稱和密碼。Run Step 2 again, and pay close attention to the user name and password that you enter.

  • 適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組要求在您的電腦上啟用 Microsoft .NET Framework 3.5. x。您的電腦可能已安裝了較新的版本 (例如 4 或 4.5.* x*).The Microsoft Azure Active Directory Module for Windows PowerShell requires that Microsoft .NET Framework 3.5. x is enabled on your computer**. It's likely that your computer has a newer version installed (for example, 4 or 4.5.* x*). 但可以啟用或停用舊版 .NET Framework 的回溯相容性。But backward compatibility with older versions of the .NET Framework can be enabled or disabled. 如需詳細資訊,請參閱下列文章:For more information, see the following articles:

  • 您的適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組版本可能已過時。Your version of the Microsoft Azure Active Directory Module for Windows PowerShell might be out of date. 若要檢查,請在適用於 Microsoft 365 的 PowerShell 或適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組中執行下列命令:To check, run the following command in PowerShell for Microsoft 365 or the Microsoft Azure Active Directory Module for Windows PowerShell:

    (Get-Item C:\Windows\System32\WindowsPowerShell\v1.0\Modules\MSOnline\Microsoft.Online.Administration.Automation.PSModule.dll).VersionInfo.FileVersion
    

    如果傳回的版本號碼低於 1.0.8070.2,請將適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組解除安裝,然後從以上的 步驟 1 中安裝。If the version number returned is lower than 1.0.8070.2, uninstall the Microsoft Azure Active Directory Module for Windows PowerShell and install from Step 1, above.

  • 如果您收到連線錯誤,請參閲 「Connect-MsolService:擲回類型例外狀況」錯誤If you get a connection error message, see "Connect-MsolService: Exception of type was thrown" error.

  • 訊息 如果您收到「Get-Item:找不到路徑」錯誤訊息,請執行此命令:If you get a "Get-Item: Cannot find path" error message, run this command:

       (dir "C:\Program Files\WindowsPowerShell\Modules\MSOnline").Name
    

另請參閱See also