PowerShell を使用してセキュリティ グループを管理する
この記事は、Microsoft 365 Enterprise および Office 365 Enterprise の両方に適用されます。
PowerShell for Microsoft 365 は、セキュリティ グループを管理するためのMicrosoft 365 管理センターの代わりに使用できます。
この記事では、設定の一覧表示、作成、変更、セキュリティ グループの削除について説明します。
この記事のコマンド ブロックで変数値を指定する必要がある場合は、次の手順を使用します。
- コマンド ブロックをクリップボードにコピーし、メモ帳または PowerShell 統合スクリプト環境 (ISE) に貼り付けます。
- 変数の値を入力し、"" 文字と "<>" 文字を削除します。
- PowerShell ウィンドウまたは PowerShell ISE でコマンドを実行します。
PowerShell でグループ メンバーシップを管理するには、「 セキュリティ グループ メンバーシップを維持 する」を参照してください。
Microsoft Graph PowerShell を使用してセキュリティ グループを管理する
注:
Azure Active Directory モジュールは、Microsoft Graph PowerShell SDK に置き換えられます。 Microsoft Graph PowerShell SDK を使用して、すべての Microsoft Graph API にアクセスできます。 詳細については、「Microsoft Graph PowerShell SDK の使用を開始する」 を参照してください。
セキュリティ グループを管理するには、Group.ReadWrite.All アクセス許可スコープ、または参照ページの [サブスクライブ済みSkus の一覧表示] に一覧表示されている他のアクセス許可のいずれかが必要Graph API。 この記事の一部のコマンドでは、異なるアクセス許可スコープが必要になる場合があります。その場合は、関連するセクションで説明します。
Connect-Graph -Scopes Group.ReadWrite.All
グループを一覧表示する
このコマンドを使用して、すべてのグループを一覧表示します。
Get-MgGroup -All
これらのコマンドを使用して、特定のグループの設定を表示名で表示します。
$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }
新しいグループを作成する
このコマンドを使用して、新しいセキュリティ グループを作成します。
Connect-MgGraph -Scopes "Group.Create"
New-MgGroup -Description "<group purpose>" -DisplayName "<name>" -MailEnabled:$false -SecurityEnabled -MailNickname "<email name>"
グループの設定を表示する
これらのコマンドを使用して、グループの設定を表示します。
$groupName="<display name of the group>"
Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName } | Select-Object *
セキュリティ グループを削除する
セキュリティ グループを削除するには、次のコマンドを使用します。
$groupName="<display name of the group>"
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
Remove-MgGroup -GroupId $group.Id
セキュリティ グループの所有者を管理する
これらのコマンドを使用して、セキュリティ グループの現在の所有者を表示します。
$groupName="<display name of the group>"
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "GroupMember.Read.All"
# Display group owners
Get-MgGroupOwner -GroupId (Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }).Id
これらのコマンドを使用して、ユーザー プリンシパル名 (UPN) でユーザー アカウントをセキュリティ グループの現在の所有者に追加します。
$userUPN="<UPN of the user account to add>"
$groupName="<display name of the group>"
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "User.ReadBasic.All"
# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'"
$userId = (Get-MgUser -Filter "userPrincipalName eq '$userUPN'").Id
# Add the user as an owner to the group
$newGroupOwner =@{
"@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
}
New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner
これらのコマンドを使用して、表示 名 でユーザー アカウントをセキュリティ グループの現在の所有者に追加します。
$userName="<Display name of the user account to add>"
$groupName="<display name of the group>"
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.Read.All", "User.ReadBasic.All"
# Get the group and user
$group = Get-MgGroup -All | Where-Object { $_.DisplayName -eq $groupName }
$userId = (Get-MgUser -All | Where-Object { $_.DisplayName -eq $userName }).Id
# Add the user as an owner to the group
$newGroupOwner =@{
"@odata.id"= "https://graph.microsoft.com/v1.0/users/$userId"
}
New-MgGroupOwnerByRef -GroupId $group.Id -BodyParameter $newGroupOwner
これらのコマンドを使用して、セキュリティ グループの現在の所有者から UPN によってユーザー アカウントを削除します。
$userUPN="<UPN of the user account to remove>"
$groupName="<display name of the group>"
# Connect to Microsoft Graph
Connect-MgGraph -Scopes "Group.ReadWrite.All", "Directory.ReadWrite.All"
# Get the group and user
$group = Get-MgGroup -Filter "displayName eq '$groupName'" | Select-Object -First 1
$user = Get-MgUser -Filter "userPrincipalName eq '$userUPN'" | Select-Object -First 1
# Remove the user from the group
Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id
これらのコマンドを使用して、ユーザー アカウントを表示 名 でセキュリティ グループの現在の所有者から削除します。
$userName="<Display name of the user account to remove>"
$groupName="<display name of the group>"
$group = Get-MgGroup | Where-Object { $_.DisplayName -eq $groupName }
$user = Get-MgUser | Where-Object { $_.DisplayName -eq $userName }
Remove-MgGroupOwnerByRef -GroupId $group.Id -DirectoryObjectId $user.Id
関連項目
Microsoft 365 ユーザー アカウント、ライセンス、PowerShell を使用したグループを管理する
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示