Manage SharePoint Online site groups with Office 365 PowerShell

 

**上次修改主題的時間:**2016-08-09

Summary: Use Office 365 PowerShell to manage your SharePoint Online site groups.

Although you can use the Office 365 系統管理中心, you can also use Office 365 PowerShell to manage your SharePoint Online site groups.

Before you begin

The procedures in this topic require you to connect to SharePoint Online. For instructions, see Connect to SharePoint Online PowerShell.

View SharePoint Online with Office 365 PowerShell

The SharePoint Online Admin center has some easy-to-use methods for managing site groups. For 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. From the Office 365 系統管理中心, click Resources > Sites, and then click the URL of the site.

  2. In the site collection dialog box, click Go to this site.

  3. On the site page, click the Settings icon (located in the upper right-hand corner of the page) and then click Site settings:

    在 SharePoint Online 系統管理中心內,存取 SharePoint Online 網站設定的範例。

  4. On the Site Settings page, click Sites permissions under Users and Permissions.

And then repeat the process for the next site you want to look at.

To get a list of the groups with Office 365 PowerShell, you would use the following command set:

$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
    }

There are two ways to run this command set in the SharePoint Online Management Shell command prompt:

  • 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.

    When you do, PowerShell will stop at a >> prompt. Press Enter to execute the foreach command.

  • 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.

    Next, run the script from the SharePoint Online Management Shell command prompt by specifying its path and file name. Here is an example:

    C:\Scripts\SiteGroupsAndUsers.ps1
    

In both cases, you should see something similar to this:

顯示成員、擁有人和訪客的 SiteGroupsAndUsers.ps1 指令碼顯示範例。

These are all the groups that have been created for the site https://litwareinc.sharepoint.com/sites/finance, as well as all the users assigned to those groups. The group names are in yellow to help you separate group names from their members.

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
            }
    }

請參閱

使用 Office 365 PowerShell 管理 SharePoint Online
使用 Office 365 PowerShell 管理 Office 365
開始使用 Office 365 PowerShell