简体   繁体   中英

Powershell remove user from specific group in sharepoint

I am trying to remove a specific user from sharepoint and have hit a wall.

I keep getting the following error

You cannot call a method on a null-valued expression.

Code:

function verifyUsers
{
    $verify_sitepath="https://extranet.mydomain.com"
    $verify_site=new-object Microsoft.SharePoint.SPSite($verify_sitepath)
    $verify_web=$verify_site.Rootweb
    $verify_web.site.url
    $verify_groups = $verify_web.groups | ? {$_.Name -match "^.*$CurrentGroup" }
    foreach($verify_group in $verify_groups)
    {
        #Write-Output "+ $($verify_group.Name)"
        foreach($verify_user in $verify_group.users)
        {
            $verify_user = $verify_user -replace "MYDOM\\",""
            Write-Output "$verify_user" | Out-File -filepath "$splist$currentGroup.txt" -append
        }
    }
    $strReference = get-Content "C:\Powershell\Users from SharePoint\$currentgroup.txt"
    $strDifference = get-Content "C:\Powershell\Users in groups\$currentgroup.txt"
    Compare-Object $strReference $strDifference

    Compare-Object $strReference $strDifference | `
    Where-Object { $_.SideIndicator -eq "<=" } | `
    ForEach-Object {
        if ($_.InputObject -eq "testuser ")
        {
            Write-Host "testuser HAHAHAHAHA"
            $TheNewGroup = $OpenWeb.SiteGroups | Where-Object {($_.Name -match $currentGroup)}
            "group Name: $TheNewGroup.Name"
            #$TheNewGroup.deleteuser("MYDOM\" + $_.InputObject)
            #$web.Update()

            $TheNewGroup.SiteUsers.Remove("MYDOM\testuser ")
        }
        Write-Host $_.InputObject
        "Deleting user: {0} from $currentgroup" -f $_.InputObject
    }
}

Found the anwser (Sorry about my messy code above)

$theuser = $verify_web.AllUsers.Item("MYDOM\testuser")
$TheNewGroup.RemoveUser($theuser)

Well, there are multiple ways to Delete Users from SharePoint Site Collection:

  1. You can delete users using SharePoint Web Interface
  2. Delete users from SharePoint site using PowerShell (Bulk Delete also possible)
  3. Remove users from SharePoint programmatically using C#

Found various possible ways at SharePointDiary.com: Delete Users from SharePoint Site Collection

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM