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

[玩转系统] 如何使用 PowerShell 从 Office 365 组中删除用户?

作者:精品下载站 日期:2024-12-14 21:13:24 浏览:14 分类:玩电脑

如何使用 PowerShell 从 Office 365 组中删除用户?


要求:使用 PowerShell 从 Office 365 组中删除用户。

如何从 Office 365 组中删除用户?

Microsoft 365 组是组织内部协作和沟通的重要工具。如果您是 Office 365 管理员,有时您可能需要从组中删除用户。本文将向你展示如何使用 PowerShell 和适用于 PowerShell 的 Azure Active Directory 模块从 Office 365 组中删除用户。我们还将向您展示如何使用 Microsoft 365 管理中心从 Office 365 组中删除用户。

使用 Microsoft 365 管理中心从 Microsoft 365 组中删除用户

您可以以管理员身份通过 Microsoft 365 管理中心从任何 Office 365 组中删除成员。具体方法如下:

  1. 登录 Microsoft 365 管理中心站点:https://admin.microsoft.com
  2. 展开“团队和组”,然后单击左侧导航中的“活跃团队和组”。
  3. 搜索并选择您想要删除成员的 Office 365 组。
  4. 在群组详细信息页面上,单击“成员”选项卡>>选择要删除的用户。
  5. 现在,您可以通过单击每个成员旁边的“删除为成员”按钮来删除群组成员。您还可以选择多个用户并一键删除他们。同样,您可以单击“所有者”选项卡来删除群组所有者。

    [玩转系统] 如何使用 PowerShell 从 Office 365 组中删除用户?

  6. 完成后点击关闭!

如何删除 Microsoft 365 用户的组成员身份?

您可以继续通过特定用户并删除组成员身份,而不是通过组。具体方法如下:

  1. 在 Office 365 中打开管理应用程序 >> 在左侧导航窗格中,选择用户 >> 活动用户。
  2. 搜索要删除的用户,从用户对象中选择三个点,然后选择“管理组”。
  3. 在下一个窗口中,选择您要删除的组,然后单击“删除”按钮并确认提示。

    [玩转系统] 如何使用 PowerShell 从 Office 365 组中删除用户?

PowerShell 从 Office 365 组中删除用户

从 Office 365 组中删除用户的一种快速简便的方法是使用 Azure AD PowerShell。以下是我们如何使用 PowerShell 从 Office 365 组中删除用户:


#Variables
$GroupName = "IT Team"
$UserUPN = "[email protected]"

#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential)
 
#Get the Azure AD Group
$AADGroup = Get-AzureADGroup -Filter "DisplayName eq '$GroupName'"

#Get the Azure AD User
$AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UserUPN'"

#Remove User from Group
Remove-AzureADGroupMember -ObjectId $AADGroup.ObjectID  -MemberId $AADUser.ObjectID 

同样,您可以通过 PowerShell 快速从所有 Office 365 组中删除用户,如下所示:


#Variables
$UserUPN = "[email protected]"

#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential) | Out-Null
 
#Get all Azure AD Unified Groups
$AADGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All:$true

#Get the Azure AD User
$AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UserUPN'"

#Check each group for the user
ForEach ($Group in $AADGroups) 
{
    $GroupMembers = (Get-AzureADGroupMember -ObjectId $Group.id).UserPrincipalName
    If ($GroupMembers -contains $UserUPN)
    {
        #Remove user from Group
        Remove-AzureADGroupMember -ObjectId $Group.Id -MemberId $AADUser.ObjectId 
        Write-Output "$UserUPN was removed from $($Group.DisplayName)"
    }
}

如何从 CSV 文件中删除 Microsoft 365 组中的多个用户?我的 CSV 文件有一个“UPN”列,其中包含用户的登录 ID。


#Variables
$CSVFile = "C:\Temp\UserList.csv"
 
#Connect to AzureAD
Connect-AzureAD -Credential (Get-Credential) | Out-Null
  
#Get all Azure AD Unified Groups
$AADGroups = Get-AzureADMSGroup -Filter "groupTypes/any(c:c eq 'Unified')" -All:$true

#Iterate through each line in CSV
Import-CSV $CSVFile | ForEach-Object {
    #Get the UPN
    $UPN = $_.UPN
    
    #Get the Azure AD User
    $AADUser  = Get-AzureADUser -Filter "UserPrincipalName eq '$UPN'"

    #Check each group for the user
    ForEach ($Group in $AADGroups) 
    {
        $GroupMembers = (Get-AzureADGroupMember -ObjectId $Group.id).UserPrincipalName
        If ($GroupMembers -contains $UPN)
        {
            #Remove user from Group
            Remove-AzureADGroupMember -ObjectId $Group.Id -MemberId $AADUser.ObjectId
            Write-Output "$UPN is removed from Group '$($Group.DisplayName)'"
        }
    }
}

使用 Exchange Online PowerShell 删除 Office 365 组成员

我们还可以使用 Exchange Online PowerShell 删除 Microsoft 365 组成员:


#Connect to Exchange Online
Connect-ExchangeOnline -Credential (Get-Credential) -ShowBanner:$false
 
#PowerShell to remove a Member from office 365 group
Remove-UnifiedGrouplinks -Identity "[email protected]" -LinkType "Members" -Links "[email protected]"

PnP PowerShell 从 Office 365 组中删除成员

使用Remove-PnPMicrosoft365GroupMember cmdlet 从 Microsoft 365 组中删除成员。


#Config Variables
$AdminSiteURL = "https://crescent-admin.sharepoint.com"
$GroupEmail = "[email protected]"
$MemberToRemove = "[email protected]"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get the Office 365 Group from Email
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail

    #Check if group exists
    If($Group -ne $Null)
    {
        #Get Group Members
        $GroupMembers = Get-PnPMicrosoft365GroupMembers -Identity $Group | Select -ExpandProperty Email

        If($GroupMembers -contains $MemberToRemove)
        {
            #Remove Member from Office 365 group
            Remove-PnPMicrosoft365GroupMember -Identity $Group -Users $MemberToRemove
            Write-Host "Member removed from the Group Successfully!" -f Green

        }
        Else
        {
            Write-Host "Could not find Member in the Group!" -f Yellow
        }
    }
    Else
    {
        Write-host "Could not Find Group!" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

如果要删除 Microsoft 365 组中的所有成员,请使用 cmdlet:Clear-PnPMicrosoft365GroupMember。

同样,您可以使用Remove-PnPMicrosoft365GroupOwner cmdlet 删除组的所有者:


#Config Variables
$AdminSiteURL = "https://salaudeen-admin.sharepoint.com"
$GroupEmail = "[email protected]"
$OwnersToRemove = "[email protected]"

Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $AdminSiteURL -Interactive

    #Get the Office 365 Group from Email
    $Group = Get-PnPMicrosoft365Group | Where Mail -eq $GroupEmail

    #Check if group exists
    If($Group -ne $Null)
    {
        #Get Group Owners
        $GroupOwners = Get-PnPMicrosoft365GroupOwner -Identity $Group | Select -ExpandProperty Email

        If($GroupOwners -contains $OwnersToRemove)
        {
            #Remove Owner from Office 365 group
            Remove-PnPMicrosoft365GroupOwner -Identity $Group -Users $OwnersToRemove
            Write-Host "Owner removed from the Group Successfully!" -f Green

        }
        Else
        {
            Write-Host "Could not find Owner in the Group!" -f Yellow
        }
    }
    Else
    {
        Write-host "Could not Find Group!" -f Yellow
    }
}
Catch {
    write-host -f Red "Error:" $_.Exception.Message
}

PnP PowerShell 从 SharePoint Online 网站的关联组中删除用户

当网站与 Microsoft 365 组连接时,可以在组级别管理权限。例如,要从网站中删除用户,您必须将其从组中删除。


#Parameters
$SiteURL = "https://crescent.sharepoint.com/sites/CorporateBranding"
$UserId= "[email protected]"
 
Try {
    #Connect to PnP Online
    Connect-PnPOnline -Url $SiteURL -Interactive

    #Get the Site
    $Site = Get-PnPSite -Includes GroupId

    #Remove user from the Site's associated Microsoft 365 Group
    Remove-PnPMicrosoft365GroupMember -Identity $Site.GroupId -Users $UserId
    Write-host "Removed User from the Associated Microsoft 365 Group!" -f Green    
}
Catch {
    Write-host -f Red "Error:" $_.Exception.Message
}

同样,要从组所有者中删除用户,请使用以下命令:


Remove-PnPMicrosoft365GroupOwner -Identity $Site.GroupId -Users $UserId

包起来

总之,从 Microsoft 365 组中删除用户是一个简单的过程,可以使用 Microsoft 365 管理中心或 PowerShell 来完成。通过删除不再属于组织或不再需要访问组资源的用户,管理员可以确保组中包含的信息的安全性和隐私性。

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

取消回复欢迎 发表评论:

关灯