分享方式:


使用 PowerShell 維護安全組成員資格

本文適用於 Microsoft 365 企業版和 Office 365 企業版。

您可以使用適用於 Microsoft 365 的 PowerShell 作為 Microsoft 365 系統管理中心 的替代方案,以維護 Microsoft 365 中的安全組成員資格。

針對 Graph 模組,請使用 Azure Active Directory PowerShell

首先, 聯機到您的 Microsoft 365 租使用者

新增或移除用戶帳戶作為群組的成員

注意事項

自 2024 年 3 月 30 日起,Azure AD 和 MSOnline PowerShell 模組已被取代。 若要深入瞭解,請閱讀 淘汰更新。 在此日期之後,這些模組的支援僅限於 Microsoft Graph PowerShell SDK 的移轉協助和安全性修正。 已淘汰的模組會繼續運作到 2025 年 3 月 30 日。

建議您移轉至 Microsoft Graph PowerShell,以與 Microsoft Entra ID (先前的 Azure AD) 互動。 如需常見的移轉問題,請參閱 移轉常見問題注意: 1.0.x 版的 MSOnline 可能會在 2024 年 6 月 30 日之後中斷。

若要依 UPN 新增使用者帳戶,請填入使用者帳戶用戶主體名稱 (UPN) (範例: belindan@contoso.com) 和安全組顯示名稱、移除 “<” 和 “>” 字元,然後在 PowerShell 視窗或 PowerShell 整合式腳本環境 (ISE) 中執行這些命令。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依顯示名稱新增用戶帳戶,請填入使用者帳戶顯示名稱 (範例:Belinda Newman) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userName="<display name of the user account to add>"
$groupName="<display name of the group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADUser | Where { $_.DisplayName -eq $userName }).ObjectID -ObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依 UPN 移除使用者帳戶,請填寫使用者帳戶 UPN (範例: belindan@contoso.com) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依顯示名稱移除用戶帳戶,請填入使用者帳戶顯示名稱 (範例:Belinda Newman) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userName="<display name of the user account to remove>"
$groupName="<display name of the group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADUser | Where { $_.DisplayName -eq $userName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

新增或移除群組作為群組的成員

安全組可以包含其他群組作為成員。 不過,Microsoft 365 群組無法。 本節包含僅針對安全組新增或移除群組的 PowerShell 命令。

若要依其顯示名稱新增群組,請填入您要新增之群組的顯示名稱,以及將包含成員群組的群組顯示名稱,然後在PowerShell視窗或PowerShell ISE 中執行這些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Add-AzureADGroupMember -RefObjectId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依其顯示名稱移除群組,請填入您要移除之群組的顯示名稱,以及將包含成員群組的群組顯示名稱,然後在PowerShell視窗或PowerShell ISE 中執行這些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Remove-AzureADGroupMember -MemberId (Get-AzureADGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -ObjectID (Get-AzureADGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

使用 Microsoft Azure Active Directory 模組進行 Windows PowerShell

首先, 聯機到您的 Microsoft 365 租使用者

新增或移除用戶帳戶作為群組的成員

若要依 UPN 新增使用者帳戶,請填入使用者帳戶用戶主體名稱 (UPN) (範例: belindan@contoso.com) 和群組顯示名稱、移除 “<” 和 “>” 字元,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依顯示名稱新增用戶帳戶,請填入使用者帳戶顯示名稱 (範例:Belinda Newman) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userName="<display name of the user account to add>"
$groupName="<display name of the group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.DisplayName -eq $userName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依 UPN 移除使用者帳戶,請填寫使用者帳戶 UPN (範例: belindan@contoso.com) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.UserPrincipalName -eq $userUPN }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

若要依顯示名稱移除用戶帳戶,請填入使用者帳戶顯示名稱 (範例:Belinda Newman) 和群組顯示名稱,然後在 PowerShell 視窗或 PowerShell ISE 中執行這些命令。

$userName="<display name of the user account to remove>"
$groupName="<display name of the group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolUser | Where { $_.DisplayName -eq $userName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID

新增或移除群組作為群組的成員

安全組可以包含其他群組作為成員。 不過,Microsoft 365 群組無法。 本節包含僅針對安全組新增或移除群組的 PowerShell 命令。

若要依其顯示名稱新增群組,請填入您要新增之群組的顯示名稱,以及將包含成員群組的群組顯示名稱,然後在PowerShell視窗或PowerShell ISE 中執行這些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group that will contain the member group>"
Add-MsolGroupMember -GroupMemberObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID -GroupMemberType Group

若要依其顯示名稱移除群組,請填入您要移除之群組的顯示名稱,以及將包含成員群組的群組顯示名稱,然後在PowerShell視窗或PowerShell ISE 中執行這些命令。

$groupMemberName="<display name of the group to add>"
$groupName="<display name of the group contains the member group>"
Remove-MsolGroupMember -GroupMemberObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupMemberName }).ObjectID -GroupObjectId (Get-MsolGroup | Where { $_.DisplayName -eq $groupName }).ObjectID -GroupMemberType Group

另請參閱

以 PowerShell 管理 Microsoft 365 使用者帳戶、授權和群組

使用 PowerShell 管理 Microsoft 365

開始使用適用於 Microsoft 365 的 PowerShell