当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 如何将成员添加到 Microsoft 365 组

作者:精品下载站 日期:2024-12-14 05:51:48 浏览:14 分类:玩电脑

如何将成员添加到 Microsoft 365 组


Microsoft 365 组允许一组人员发送电子邮件并共享文件、日历和其他资源。有时您必须更新 Microsoft 365 组成员。在本文中,你将了解如何在 Microsoft Entra 管理中心和 PowerShell 中向 Microsoft 365 组添加成员。

Microsoft 365 组

每个组织最多可以在 Microsoft 365 中创建 500,000 个组。

Microsoft 365 GroupMaximum valueNumber of owners per group100Number of members per groupMore than 1,000*Single user can create groups250Admin can create groups500,000 (default tenant limit)Number of groups a user can be a member of1,000File storage1 TB
10 GB per subscribed user
Additional storage purchased (unlimited)Mailbox size50 GB

*只有1,000人可以一起访问群组对话。

注意:用户在 Outlook 中访问非常大的组中的日历和对话时可能会注意到延迟。

如果没有 Microsoft 365 组,请了解如何在 Microsoft Entra 管理中心和使用 Microsoft Graph PowerShell 创建 Microsoft 365 组。

将成员添加到 Microsoft Entra ID 中的 Microsoft 365 组

要将成员添加到 Microsoft Entra 管理中心的 Microsoft 365 组,请按照以下步骤操作:

  1. 登录 Microsoft Entra 管理中心
  2. 展开身份 > 组 > 所有组
  3. 单击列表中的 Microsoft 365 组

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 点击成员

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 点击添加成员

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 选择用户
  2. 点击选择

[玩转系统] 如何将成员添加到 Microsoft 365 组

您已成功将组成员添加到 Microsoft 365 组。

注意:在 Microsoft Entra 管理中心中,您最多可以选择 100 个用户并将他们添加为成员。因此,如果您想向群组添加超过 100 名成员,则需要重复此步骤。另一种方法是使用 PowerShell 来避免这种情况。

使用 PowerShell 将成员添加到 Microsoft 365 组

我们将展示如何使用 PowerShell 将成员添加到现有 Microsoft 365 组:

  • 将单个成员添加到 Microsoft 365 组
  • 将多个成员添加到单个 Microsoft 365 组
  • 将批量成员添加到单个 Microsoft 365 组
  • 将多个成员添加到所有 Microsoft 365 组
  • 将单个成员添加到所有 Microsoft 365 组

为此,我们需要使用 New-MgGroupMember PowerShell cmdlet。

连接到 Microsoft Graph PowerShell

在开始之前,您必须安装 Microsoft Graph PowerShell 模块。以管理员身份启动 Windows PowerShell 并运行以下命令。

Install-Module Microsoft.Graph -Force

重要提示:在运行 cmdlet 或脚本之前,请务必安装最新的 Microsoft Graph PowerShell 模块版本,以防止出现错误和不正确的结果。

使用以下范围运行 Connect-MgGraph cmdlet 以通过 Microsoft Graph 进行身份验证。

Connect-MgGraph -Scopes "User.ReadWrite.All", "Group.ReadWrite.All", "GroupMember.ReadWrite.All"

现在一切就绪,您可以通过 Microsoft Graph PowerShell 使用这些命令。

将成员添加到 Microsoft 365 组

您可以使用 New-MgGroupMember PowerShell cmdlet 通过 MS Graph PowerShell 将单个或多个成员添加到 Microsoft 365 组。

在我们的示例中,我们将向 Microsoft 365 组(管理团队)添加多个成员。

请参阅将成员添加到 Microsoft 365 组的 PowerShell 语法。

New-MgGroupMember -GroupId "Group Object Id" -DirectoryObjectId "User Object ID"

运行以下 PowerShell 命令将成员添加到 Microsoft 365 组。

New-MgGroupMember -GroupId "031f52ea-cb97-40fc-ab0f-73598f3fc66d" -DirectoryObjectId "41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5"

将多个成员添加到 Microsoft 365 组

要将多个成员添加到 Microsoft 365 组,您还可以指定 UserPrincipalName 而不是对象 ID。

  1. 在脚本顶部键入要添加的用户的 UPN
  2. 输入组对象 ID
  3. 运行以下 PowerShell 脚本
$upns = @(
    "[email protected]",
    "[email protected]",
    "[email protected]",
)
$GroupId = "031f52ea-cb97-40fc-ab0f-73598f3fc66d"

$Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

if ($Group) {

    foreach ($upn in $upns) {
        $User = Get-MgUser -Filter "userPrincipalName eq '$upn'"
        
        if ($User) {
            $UserId = $User.Id
            $ExistingMember = Get-MgGroupMember -GroupId $GroupId
            
            if ($ExistingMember.Id -eq $UserId) {
                Write-Host "User $($User.UserPrincipalName) already exists in the group." -ForegroundColor Yellow
            }
            else {
                New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $UserId
                Write-Host "User $($User.UserPrincipalName) added to the group." -ForegroundColor Green
            }
        }
        else {
            Write-Host "User $upn does not exist." -ForegroundColor Red
        }
    }
}
else {
    Write-Host "Group $GroupId does not exist." -ForegroundColor Red
}

将所有成员批量添加到 Microsoft 365 组

您可以将所有成员添加到单个 Microsoft 365 组。使用 MS Graph PowerShell,您可以一次添加 100 多个成员。

  1. 在第 编号 1 行中指定 Microsoft 365 组 ID
  2. 运行以下 PowerShell 脚本将所有用户添加到单个 Microsoft 365 组
$GroupId = "031f52ea-cb97-40fc-ab0f-73598f3fc66d"

$Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

if ($Group) {
    $Users = Get-MgUser -All
    
    foreach ($User in $Users) {
        $ExistingMember = Get-MgGroupMember -GroupId $GroupId
        
        if ($ExistingMember.Id -eq $User.Id) {
            Write-Host "User $($User.UserPrincipalName) already exists in the group." -ForegroundColor Yellow
        }
        else {
            New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $User.Id
            Write-Host "User $($User.UserPrincipalName) added to the group." -ForegroundColor Green
        }
    }
}
else {
    Write-Host "Group $GroupId does not exist." -ForegroundColor Red
}

将特定部门的用户添加到 Microsoft 365 组

在我们的示例中,我们希望将营销部门的所有用户添加到 Microsoft 365 组([email protected])。

  1. 第 1 行中键入 Microsoft 365 组 ID
  2. 运行以下 PowerShell 脚本
$GroupId = "031f52ea-cb97-40fc-ab0f-73598f3fc66d"

$Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

if ($Group) {
    $Users = Get-MgUser -All -Filter "Department eq 'Marketing'"
    
    foreach ($User in $Users) {
        $ExistingMember = Get-MgGroupMember -GroupId $GroupId
        
        if ($ExistingMember.Id -eq $User.Id) {
            Write-Host "User $($User.UserPrincipalName) already exists in the group." -ForegroundColor Yellow
        }
        else {
            New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $User.Id
            Write-Host "User $($User.UserPrincipalName) added to the group." -ForegroundColor Green
        }
    }
}
else {
    Write-Host "Group $GroupId does not exist." -ForegroundColor Red
}

它将把营销部门的用户添加到 Microsoft 365 组 ([email protected])。

从 CSV 将多个成员添加到 Microsoft 365 组

您可以从 CSV 文件将多个成员添加到单个 Microsoft 365 组。

创建一个包含两列的 CSV 文件:

  1. 打开Microsoft Excel
  2. 输入 UPN 作为第一列的标题
  3. 指定UserPrincipalName
  4. 输入 UserID 作为第二列的标题
  5. 指定成员对象 ID

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 如果 (C:) 驱动器中还没有文件夹 temp,请创建该文件夹
  2. 将文件命名为 M365users.csv
  3. 保存类型为 CSV(逗号分隔 (*.csv)
  4. 点击保存

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 第 1 行中指定CSV 文件路径
  2. 第 2 行中键入 Microsoft 365 组 ID
  3. 运行以下 PowerShell 脚本
$CsvFilePath = "C:\temp\M365users.csv"
$GroupId = "031f52ea-cb97-40fc-ab0f-73598f3fc66d"

$Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

if ($Group) {
    $CsvUsers = Import-Csv -Path $CsvFilePath

    foreach ($CsvUser in $CsvUsers) {
        $CsvUserID = $CsvUser.UserId
        $User = Get-MgUser -Filter "Id eq '$CsvUserID'" -ErrorAction SilentlyContinue
         
        if ($User) {
            $UserId = $User.Id
            $ExistingMember = Get-MgGroupMember -GroupId $GroupId
               
            if ($ExistingMember.Id -eq $UserId) {
                Write-Host "User $($User.UserPrincipalName) already exists in the group." -ForegroundColor Yellow
            }
            else {
                New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $UserId
                Write-Host "User $($User.UserPrincipalName) added to the group." -ForegroundColor Green
            }
        }
        else {
            Write-Host "User $CsvUserID does not exist." -ForegroundColor Red
        }
    }
}
else {
    Write-Host "Group $GroupId does not exist." -ForegroundColor Red
}

PowerShell 输出结果显示是否:

  • Microsoft 365 组不存在或拼写错误
  • 用户已经是 Microsoft 365 组的成员
  • 用户已添加到 Microsoft 365 组
  • 用户不存在或拼写错误

从 CSV 将成员添加到多个 Microsoft 365 组

您可以从 CSV 文件将现有用户添加到多个 Microsoft 365 组。您需要创建一个包含两列的 CSV 文件。前两列将包含 Microsoft 365 组的列表。第三列和第四列将包含您要添加为成员的用户列表。

创建一个包含四列的 CSV 文件:

  1. 打开Microsoft Excel
  2. 输入 GroupName 作为第一列的标题
  3. 指定 Microsoft 365 组电子邮件
  4. 输入 GroupID 作为第二列的标题
  5. 指定 Microsoft 365组 ID
  6. 输入用户名作为第三列的标题
  7. 指定成员UserPrincipalName
  8. 输入 MemberId 作为第四列的标题
  9. 指定成员对象 ID

请参阅下面的 CSV 文件示例。

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 如果 (C:) 驱动器中还没有文件夹 temp,请创建该文件夹
  2. 将文件命名为 M365members.csv
  3. 保存类型为 CSV(逗号分隔 (*.csv)
  4. 点击保存

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 第 1 行中指定CSV 文件路径
  2. 运行以下 PowerShell 脚本
$CsvFilePath = "C:\temp\M365members.csv"

$Csv = Import-Csv $CsvFilePath

foreach ($row in $Csv) {
    $GroupId = $Row.GroupId
    $MemberId = $Row.MemberId

    $Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

    if ($Group) {
        $ExistingMember = Get-MgGroupMember -GroupId $GroupId
    
        if ($ExistingMember.Id -eq $MemberId) {
            Write-Host "User $MemberId already exists in the group." -ForegroundColor Yellow
        }
        else {
            $User = Get-MgUser -Filter "Id eq '$MemberId'" -ErrorAction SilentlyContinue
    
            if ($User) {
                New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $MemberId
                Write-Host "User $MemberId added to the group." -ForegroundColor Green 
            }
            else {
                Write-Host "User $MemberId doesn't exist" -ForegroundColor Red
            }
        }
    }
    else {
        Write-Host "Group $GroupId does not exist." -ForegroundColor Red
    }
}

PowerShell 输出结果显示是否:

  • Microsoft 365 组不存在或拼写错误
  • 用户已经是 Microsoft 365 组的成员
  • 用户已添加到 Microsoft 365 组
  • 用户不存在或拼写错误

从 CSV 将单个用户添加到所有 Microsoft 365 组

要将用户添加到多个 Microsoft 365 组,您需要创建 CSV 文件。

在我们的示例中,我们希望将用户(Amanda Hansen)添加到多个 Microsoft 365 组。

创建一个包含两列的 CSV 文件:

  1. 打开Microsoft Excel
  2. 输入作为第一列的标题
  3. 指定 Microsoft 365 组主 SMTP 地址
  4. 输入 ObjectID 作为第二列的标题
  5. 指定 Microsoft 365 组对象 ID

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 如果 (C:) 驱动器中还没有文件夹 temp,请创建该文件夹
  2. 将文件命名为M365groups.csv
  3. 保存类型为 CSV(逗号分隔 (*.csv)
  4. 点击保存

[玩转系统] 如何将成员添加到 Microsoft 365 组

  1. 第 1 行中指定CSV 文件路径
  2. 第 2 行中输入用户对象 ID
  3. 运行以下 PowerShell 脚本
$CsvFilePath = "C:\temp\M365groups.csv"
$UserId = "41377e9c-dc47-46c0-b4a5-1d5bbdcb5cc5"

# Check if the user exists
$User = Get-MgUser -UserId $UserId -ErrorAction SilentlyContinue

if ($User) {
    $CsvGroups = Import-Csv -Path $CsvFilePath

    foreach ($row in $CsvGroups) {
        $GroupId = $Row.ObjectID

        $Group = Get-MgGroup -GroupId $GroupId -ErrorAction SilentlyContinue

        if ($Group) {
            $ExistingMember = Get-MgGroupMember -GroupId $GroupId
            
            if ($ExistingMember.Id -eq $UserId) {
                Write-Host "User $UserId already exists in the group." -ForegroundColor Yellow
            }
            else {
                New-MgGroupMember -GroupId $GroupId -DirectoryObjectId $UserId
                Write-Host "User $UserId added to the group." -ForegroundColor Green
            }
        }
        else {
            Write-Host "Group $GroupId does not exist." -ForegroundColor Red
        }
    }
}
else {
    Write-Host "User $UserId does not exist." -ForegroundColor Red
}

就是这样!

阅读更多内容:如何使用 PowerShell 将成员添加到通讯组 »

结论

您了解了如何在 Microsoft Entra 管理中心和使用 PowerShell 将成员添加到 Microsoft 365 组。可以使用这两种方法将单个、多个或所有用户添加为 Microsoft 365 组的成员。如果要将成员添加到多个 Microsoft 365 组,最好创建 CSV 文件并使用 PowerShell 脚本。

您喜欢这篇文章吗?您可能还喜欢如何导出完全访问邮箱权限。不要忘记关注我们并分享这篇文章。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯