在單一 PowerShell 視窗中連線至所有 Microsoft 365 服務Connect to all Microsoft 365 services in a single PowerShell window

使用 PowerShell 管理 Microsoft 365 時,可以同時開啟多個 PowerShell 工作階段。When you use PowerShell to manage Microsoft 365, you can have multiple PowerShell sessions open at the same time. 您可能有不同的 PowerShell 視窗來管理使用者帳戶、SharePoint Online、Exchange Online、商務用 Skype Online、Microsoft Teams 和安全性與合規性中心。You might have different PowerShell windows to manage user accounts, SharePoint Online, Exchange Online, Skype for Business Online, Microsoft Teams, and the Security & Compliance center.

此案例對於管理 Microsoft 365 而言並不理想,因為您無法在用於交叉服務管理的那些視窗之間交換資料。This scenario isn't optimal for managing Microsoft 365, because you can't exchange data among those windows for cross-service management. 本文章說明如何使用 PowerShell 的單一執行個體來管理 Microsoft 365 帳戶、商務用 Skype Online、Exchange Online、SharePoint Online、Microsoft Teams 及安全性與合規性中心。This article describes how to use a single instance of PowerShell to manage Microsoft 365 accounts, Skype for Business Online, Exchange Online, SharePoint Online, Microsoft Teams, and the Security & Compliance Center.

注意

本文目前僅包含用來連線至全球 (+GCC) 雲端的命令。This article currently only contains the commands to connect to the Worldwide (+GCC) cloud. 附註提供有關如何連線到其他 Microsoft 365 雲端之文章的連結。Notes provide links to articles about connecting to the other Microsoft 365 clouds.

開始之前Before you begin

在您可以從 PowerShell 的單一執行個體管理所有 Microsoft 365 之前,請考慮下列先決條件:Before you can manage all of Microsoft 365 from a single instance of PowerShell, consider the following prerequisites:

  • 您使用的 Microsoft 365 公司或學校帳戶必須是 Microsoft 365 系統管理員角色的成員。The Microsoft 365 work or school account that you use must be a member of a Microsoft 365 admin role. 如需詳細資訊,請參閱關於系統管理員角色For more information, see About admin roles. 這是對 Microsoft 365 的 PowerShell 需求,但並非所有其他 Microsoft 365 服務的需求。This is a requirement for PowerShell for Microsoft 365, but not necessarily for all other Microsoft 365 services.

  • 您可以使用下列 Windows 64 位元版本:You can use the following 64-bit versions of Windows:

    • Windows 10Windows 10

    • Windows 8.1 或 Windows 8Windows 8.1 or Windows 8

    • Windows Server 2019Windows Server 2019

    • Windows Server 2016Windows Server 2016

    • Windows Server 2012 R2 或 Windows Server 2012Windows Server 2012 R2 or Windows Server 2012

    • Windows 7 Service Pack 1 (SP1)*Windows 7 Service Pack 1 (SP1)*

    • Windows Server 2008 R2 SP1*Windows Server 2008 R2 SP1*

      *您需要安裝 Microsoft .NET Framework 4.5.x,然後安裝 Windows Management Framework 3.0 或4.0。* You need to install Microsoft .NET Framework 4.5.x and then Windows Management Framework 3.0 or 4.0. 如需詳細資訊,請參閱 Windows Management FrameworkFor more information, see Windows Management Framework.

      由於商務用 Skype Online 模組和其中一個 Microsoft 365 模組的需求,您需要使用 Windows 64 位元版本。You need to use a 64-bit version of Windows because of the requirements for the Skype for Business Online module and one of the Microsoft 365 modules.

  • 您需要安裝 Azure Active Directory (Azure AD)、Exchange Online、SharePoint Online、商務用 Skype Online 及 Teams 所需的模組:You need to install the modules that are required for Azure Active Directory (Azure AD), Exchange Online, SharePoint Online, Skype for Business Online and Teams:

  • 必須設定 PowerShell,才能執行商務用 Skype Online 及安全性與合規性中心的已簽署指令碼。PowerShell must be configured to run signed scripts for Skype for Business Online and the Security & Compliance Center. 在提高權限的 PowerShell 工作階段 (以系統管理員身份執行 的 PowerShell 執行階段) 中執行以下命令。Run the following command in an elevated PowerShell session (a PowerShell session that you Run as administrator).

    Set-ExecutionPolicy RemoteSigned
    

只使用一組密碼時的連線步驟Connection steps when using just a password

僅使用密碼登入時,請按照以下步驟在單一 PowerShell 視窗中連線到所有服務。Follow these steps to connect to all the services in a single PowerShell window when you're using just a password for sign-in.

  1. 開啟 [Windows PowerShell]。Open Windows PowerShell.

  2. 執行此指令並輸入您的 Microsoft 365 公司或學校帳戶憑證。Run this command and enter your Microsoft 365 work or school account credentials.

    $credential = Get-Credential
    
  3. 執行此指令,以透過使用 Azure Active Directory PowerShell for Graph 模組連線到 Azure AD。Run this command to connect to Azure AD by using the Azure Active Directory PowerShell for Graph module.

    Connect-AzureAD -Credential $credential
    

    或者如果您使用的是適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組,請執行此命令。Or if you're using the Microsoft Azure Active Directory Module for Windows PowerShell module, run this command.

    Connect-MsolService -Credential $credential
    

    注意

    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。You must run these cmdlets from PowerShell.

  4. 執行這些指令,以連線到 SharePoint Online。Run these commands to connect to SharePoint Online. 指定您網域的組織名稱。Specify the organization name for your domain. 例如,針對 "litwareinc.onmicrosoft.com",組織名稱值為 "litwareinc"。For example, for "litwareinc.onmicrosoft.com", the organization name value is "litwareinc".

    $orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
    Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
    Connect-SPOService -Url https://$orgName-admin.sharepoint.com -Credential $Credential
    
  5. 執行這些指令,以連線至商務用 Skype Online。Run these commands to connect to Skype for Business Online. 首次連線時,將出現有關增加 WSMan NetworkDelayms 值的警告。A warning about increasing the WSMan NetworkDelayms value will appear the first time that you connect. 略過它。Ignore it.

    注意

    商務用 Skype Online 連接器目前是最新 Teams PowerShell 模組的一部分。Skype for Business Online Connector is currently part of the latest Teams PowerShell module. 如果您使用的是最新的 Teams PowerShell 公開發行版本,則不需要安裝商務用 Skype Online 連接器。If you're using the latest Teams PowerShell public release, you don't need to install the Skype for Business Online Connector.

    $sfboSession = New-CsOnlineSession -Credential $credential
    Import-PSSession $sfboSession
    
  6. 執行這些命令,以連線到 Exchange Online。Run these commands to connect to Exchange Online.

    Import-Module ExchangeOnlineManagement
    Connect-ExchangeOnline -ShowProgress $true
    

    注意

    若要連線至「全球」以外適用於 Microsoft 365 的 Exchange Online 雲端,請參閱連線至 Exchange Online PowerShellTo connect to Exchange Online for Microsoft 365 clouds other than Worldwide, see Connect to Exchange Online PowerShell.

  7. 執行這些指令以連線至安全性 & 合規性中心。Run these commands to connect to the Security & Compliance Center.

    $acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
    Connect-IPPSSession -UserPrincipalName $acctName
    

    注意

    若要連線到 Microsoft 365 雲端 (而非 [Worldwide]) 安全性 & 合規性中心,請參閱 連線到安全性與合規性中心 PowerShellTo connect to the Security & Compliance Center for Microsoft 365 clouds other than Worldwide, see Connect to Security & Compliance Center PowerShell.

  8. 執行這些命令,以連線到 Teams PowerShell。Run these commands to connect to Teams PowerShell.

    Import-Module MicrosoftTeams
    Connect-MicrosoftTeams
    

    注意

    若要連線到 Microsoft Teams 雲端 (而非 [全球]),請參閱 Connect-MicrosoftTeamsTo connect to Microsoft Teams clouds other than Worldwide, see Connect-MicrosoftTeams.

Azure Active Directory PowerShell for Graph 模組Azure Active Directory PowerShell for Graph module

以下是使用適用於 Graph 的 Azure Active Directory PowerShell 模組時,單一區塊中所有服務的命令。Here are the commands for all the services in a single block when you use the Azure Active Directory PowerShell for Graph module. 指定您網域主機的名稱和用於登入的 UPN,並同時執行它們。Specify the name of your domain host and the UPN for the sign-in and run them all at the same time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$credential = Get-Credential -UserName $acctName -Message "Type the account's password."
#Azure Active Directory
Connect-AzureAD -Credential $credential
#SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
#Skype for Business Online
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -ShowProgress $true
#Security & Compliance Center
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組Microsoft Azure Active Directory Module for Windows PowerShell module

以下是使用適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組時,單一區塊中所有服務的命令。Here are the commands for all the services in a single block when you use the Microsoft Azure Active Directory Module for Windows PowerShell module. 指定您網域主機的名稱和用於登入的 UPN,並同時執行它們。Specify the name of your domain host and the UPN for the sign-in and run them all at one time.

$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$credential = Get-Credential -UserName $acctName -Message "Type the account's password."
#Azure Active Directory
Connect-MsolService -Credential $credential
#SharePoint Online
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking
Connect-SPOService -Url https://$orgName-admin.sharepoint.com -credential $credential
#Skype for Business Online
$sfboSession = New-CsOnlineSession -Credential $credential
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -ShowProgress $true
#Security & Compliance Center
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

使用多重要素驗證時的連線步驟Connection steps when using multi-factor authentication

針對 Graph 的 Azure Active Directory PowerShell 模組Azure Active Directory PowerShell for Graph module

以下是使用多重要素驗證搭配適用於 Graph 的 Azure Active Directory PowerShell 模組時,用來在單一區塊中連接至多個 Microsoft 365 服務的所有命令。Here are all the commands in a single block to connect to multiple Microsoft 365 services when you use multi-factor authentication with the Azure Active Directory PowerShell for Graph module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-AzureAD
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
$sfboSession = New-CsOnlineSession
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName $acctName -ShowProgress $true
#Security & Compliance Center
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組Microsoft Azure Active Directory Module for Windows PowerShell module

以下是使用多重要素驗證搭配適用於 Windows PowerShell 的 Microsoft Azure Active Directory 模組時,用來在單一區塊中連接至多個 Microsoft 365 服務的所有命令。Here are all the commands in a single block to connect to multiple Microsoft 365 services when you use multi-factor authentication with the Microsoft Azure Active Directory Module for Windows PowerShell module.

$acctName="<UPN of the account, such as belindan@litwareinc.onmicrosoft.com>"
$orgName="<for example, litwareinc for litwareinc.onmicrosoft.com>"
#Azure Active Directory
Connect-MsolService
#SharePoint Online
Connect-SPOService -Url https://$orgName-admin.sharepoint.com
#Skype for Business Online
$sfboSession = New-CsOnlineSession
Import-PSSession $sfboSession
#Exchange Online
Import-Module ExchangeOnlineManagement
Connect-ExchangeOnline -UserPrincipalName $acctName -ShowProgress $true
#Security & Compliance Center
Connect-IPPSSession -UserPrincipalName $acctName
#Teams
Import-Module MicrosoftTeams
Connect-MicrosoftTeams

關閉 PowerShell 視窗Close the PowerShell window

若要關閉 PowerShell 視窗,請執行此命令,以移除連線至商務用 Skype Online、SharePoint Online 及 Teams 的使用中工作階段:To close down the PowerShell window, run this command to remove the active sessions to Skype for Business Online, SharePoint Online, and Teams:

Remove-PSSession $sfboSession ; Disconnect-SPOService ; Disconnect-MicrosoftTeams 

請參閱See also