使用 PowerShell 管理 SharePoint Online 網站群組Manage SharePoint Online site groups with PowerShell

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

雖然您可以使用 Microsoft 365 系統管理中心,但也可以使用 Microsoft 365 的 PowerShell 來管理 SharePoint Online 網站群組。Although you can use the Microsoft 365 admin center, you can also use PowerShell for Microsoft 365 to manage your SharePoint Online site groups.

在您開始之前Before you begin

本文中的程式需要您連線至 SharePoint。The procedures in this article require you to connect to SharePoint Online. 如需相關指示,請參閱 Connect to SharePoint Online PowerShellFor instructions, see Connect to SharePoint Online PowerShell.

使用 Microsoft 365 的 PowerShell 觀看 SharePoint 線上View SharePoint Online with PowerShell for Microsoft 365

SharePoint Online 系統管理中心具有一些用於管理網站群組的便於使用的方法。The SharePoint Online admin center has some easy-to-use methods for managing site groups. 例如,假設您想要查看網站的群組和群組成員 https://litwareinc.sharepoint.com/sites/financeFor example, suppose you want to look at the groups, and the group members, for the https://litwareinc.sharepoint.com/sites/finance site. 以下是您必須執行的動作:Here's what you have to do to:

  1. 從 SharePoint 系統管理中心,按一下 [作用中的 網站],然後按一下網站的 URL。From the SharePoint admin center, click Active sites, and then click the URL of the site.
  2. 在 [網站] 頁面上,按一下位於頁面右上角的 [ 設定 ] 圖示 () ,然後按一下 [ 網站許可權]。On the site page, click the Settings icon (located in the upper right-hand corner of the page), and then click Site permissions.

然後針對您想要查看的下一個網站重複此程式。And then repeat the process for the next site you want to look at.

若要取得使用 Microsoft 365 PowerShell 的群組清單,您可以使用下列命令:To get a list of the groups with PowerShell for Microsoft 365, you can use the following commands:

$siteURL = "https://litwareinc.sharepoint.com/sites/finance"
$x = Get-SPOSiteGroup -Site $siteURL
foreach ($y in $x)
    {
        Write-Host $y.Title -ForegroundColor "Yellow"
        Get-SPOSiteGroup -Site $siteURL -Group $y.Title | Select-Object -ExpandProperty Users
        Write-Host
    }

在 SharePoint 線上管理命令介面命令提示字元中,有兩種方式可以執行這個命令集:There are two ways to run this command set in the SharePoint Online Management Shell command prompt:

  • 將命令複製到記事本中 (或其他文字編輯器) 、修改 $siteURL 變數的值、選取命令,然後將其貼到 SharePoint 線上管理命令介面命令提示字元中。Copy the commands into Notepad (or another text editor), modify the value of the $siteURL variable, select the commands, and then paste them into the SharePoint Online Management Shell command prompt. 當您這麼做時,PowerShell 會在 >> 提示時停止。When you do, PowerShell will stop at a >> prompt. 按 Enter 鍵以執行 foreach 命令。Press Enter to execute the foreach command.
  • 將命令複製到記事本中 (或其他文字編輯器) 、修改 $siteURL 變數的值,然後在適當的資料夾中以名稱及 ps1 副檔名儲存這個文字檔。Copy the commands into Notepad (or another text editor), modify the value of the $siteURL variable, and then save this text file with a name and the .ps1 extension in a suitable folder. 接下來,指定 SharePoint 線上管理命令介面命令提示字元,並指定其路徑和檔案名以執行腳本。Next, run the script from the SharePoint Online Management Shell command prompt by specifying its path and file name. 範例命令如下:Here is an example command:
C:\Scripts\SiteGroupsAndUsers.ps1

在這兩種情況下,您應該會看到類似以下的內容:In both cases, you should see something similar to this:

SharePoint 線上網站群組

這些是已為網站建立的所有群組 https://litwareinc.sharepoint.com/sites/finance ,以及指派給這些群組的所有使用者。These are all the groups that have been created for the site https://litwareinc.sharepoint.com/sites/finance, and all the users assigned to those groups. 組名為黃色,可協助您將群組名稱與其成員分開。The group names are in yellow to help you separate group names from their members.

在另一個範例中,這裡是列出所有 SharePoint Online 網站之群組和所有群組成員資格的命令集。As another example, here is a command set that lists the groups, and all the group memberships, for all of your SharePoint Online sites.

$x = Get-SPOSite
foreach ($y in $x)
    {
        Write-Host $y.Url -ForegroundColor "Yellow"
        $z = Get-SPOSiteGroup -Site $y.Url
        foreach ($a in $z)
            {
                 $b = Get-SPOSiteGroup -Site $y.Url -Group $a.Title 
                 Write-Host $b.Title -ForegroundColor "Cyan"
                 $b | Select-Object -ExpandProperty Users
                 Write-Host
            }
    }

請參閱See also

連線至 SharePoint 線上 PowerShellConnect to SharePoint Online PowerShell

使用 PowerShell 建立 SharePoint Online 網站並新增使用者Create SharePoint Online sites and add users with PowerShell

使用 PowerShell 管理 SharePoint Online 使用者和群組Manage SharePoint Online users and groups with PowerShell

使用 PowerShell 管理 Microsoft 365Manage Microsoft 365 with PowerShell

開始使用適用於 Microsoft 365 的 PowerShellGetting started with PowerShell for Microsoft 365