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

[玩转系统] 使用 PowerShell 从 SharePoint 网站或列表中删除所有用户

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

使用 PowerShell 从 SharePoint 网站或列表中删除所有用户


要求: 有一个包含大量用户权限的 SharePoint 2013 列表,我想从该 SharePoint 列表中删除所有用户权限。因此尝试删除所有用户,但 SharePoint 给出“您选择一次删除太多组。选择更少的组并重试。”选择“所有用户”复选框并将其从 SharePoint 网站或组中删除时。

[玩转系统] 使用 PowerShell 从 SharePoint 网站或列表中删除所有用户

那么,这里的问题是:当用户数量超过SharePoint的最大计数时,我们无法批量删除用户。以下是我的实用 PowerShell 脚本,用于从 SharePoint 网站或列表中删除所有用户。

用于从 SharePoint 列表中删除所有用户权限的 PowerShell 脚本:

让我向您展示如何使用 PowerShell 从 SharePoint 列表中删除所有用户。


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Variables
$SiteUrl="https://your-sharepoint-site.com" 
$ListName="Your-List-Name" 

#get the Web
$web = Get-SPWeb $SiteUrl
#Get the List
$list = $web.Lists[$ListName]

#If the List is not using Unique permissions, Break inheritance
if(!$list.HasUniqueRoleAssignments)
{
    #Break list inheritance - without copying users
    $list.BreakRoleInheritance($false)
}

#Remove all users from the list
 for ($i = $list.RoleAssignments.Count - 1; $i -ge 0; $i--)
    {
      $RoleAssignment = $list.RoleAssignments[$i]
      $list.RoleAssignments.RemoveByID($RoleAssignment.Member.ID)
      write-host "Removed User/Group :" $RoleAssignment.Member.name
    } 

从 SharePoint 网站中删除所有用户

同样,要从 SharePoint 网站删除所有用户,请使用以下 PowerShell:


Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Site collection URL 
$SiteUrl="https://demo.sp.cloudservicesme.com" 

#get the Root Web
$web = Get-SPWeb $SiteUrl

#Get All Users of the site collection
$UserAccounts = @()
foreach ($user in $web.SiteUsers)
{
   $UserAccounts = $UserAccounts + $user.loginname
}

#Remove all users one by one.
foreach ($user in $UserAccounts)
{
    try
    {
        #Set the Error Action
        $ErrorActionPreference = "Stop"

        #Remove User if not site admin
        if(!$web.SiteUsers[$User].isSiteAdmin)
        {
            $web.SiteUsers.Remove($user)
            Write-host "User Removed :" $user -ForegroundColor Green
        }
    }
    catch    
    {
        Write-host "Failed to remove the user:" + $user -ForegroundColor Red   
    }
    Finally
    {
        #Reset the Error Action to Default
        $ErrorActionPreference = "Continue"
    } 
}

如果您需要通过删除每个人的直接权限来清理 SharePoint 列表或网站,这会很有用。

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

取消回复欢迎 发表评论:

关灯