Quantcast
Channel: SharePoint Diary
Viewing all articles
Browse latest Browse all 1058

Delete Users from SharePoint Site Collection using PowerShell

$
0
0
So you don't want a particular user to have access to SharePoint 2010 site anymore! Want to delete user from SharePoint 2010 site collection, isn't it? well, How to delete user from site collection?
  1. To delete user in SharePoint 2010, Navigate to the site collection as a Site collection Administrator
  2. Click on Site actions >> Site permissions
  3. Click on any SharePoint group. Now the URL will look some thing like:
    http://your-site-collection-url/_layouts/people.aspx?MembershipGroupID=11.
  4. Change the MemberShipGroupID parameter's value from 11 to 0. i.e. :
    http://your-site-collection-url/_layouts/people.aspx?MembershipGroupID=0 and hit "Enter" key. This will lead you to see All People page.
  5. Select the user you want to delete, and click on Actions >> "Delete user from Site collection"
    sharepoint delete user from site collection powershell
That's it!

By the way, to delete user in SharePoint 2007, "All People" page link is straightforward. Just navigate to: Site Actions >> Site Settings >> Advanced Permissions >> All People (In Quick Launch!)

Delete Users from SharePoint Site Collection using PowerShell:
PowerShell comes really handy in occasions of repeated manual tasks. Lets use PowerShell to delete user from SharePoint 2010 site collection.

PowerShell script in SharePoint 2010 to delete user from site collection
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

#Get the Root web
$web = Get-SPWeb "http://sharepoint.crescent.com/sites/operations"
#Supply the user account 
$UserAccount = "Global\DaveP"
#Removes user from site collection
Remove-SPUser -Identity $UserAccount -Web $Web -Confirm:$False
This PowerShell script deletes a particular user from given site collection. Also it deletes user from userinfo table! Remember, SharePoint cannot delete user if the user is Site collection administrator!!

SharePoint delete user from site collection programmatically using C#:
We can also use C# object model code in SharePoint to delete user programmatically:
   //Define the parameter values: Site collection URL and user account to remove
            string siteURL = "http://sharepoint.crescent.com/sites/operations";
            string userAccount = @"Global\MarkM";

            using (SPSite site = new SPSite(siteURL))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    //Get the User
                    SPUser user=null;
                    try
                    {
                         user = web.SiteUsers[userAccount];  
                    }
                    catch (SPException ex) {//user Not found in the site collection}
                    
                    //Check if the given user is valid and found in the site collection
                    if (user !=null)
                    {
                        //Remove the user if he is not a Site collection administrator
                        if (!user.IsSiteAdmin)
                        {
                            //Remove the user from site collection
                            web.SiteUsers.Remove(user.LoginName);  
                            Console.WriteLine("User removed from:"+site.RootWeb.Url);
                        }
                    }
                }
            }
            //Pause
            Console.ReadLine();
this removes user completely from SharePoint site collection.

Delete user from all site collections in SharePoint 2010 using PowerShell:
Some times, we may need to delete a particular user from all site collections. Say for e.g. Employee leaves the company!

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue

Function DeleteUserFromAllSites([string]$WebAppURL, [string]$UserAccount, [bool]$ScanOnly)
{
 <#
  .SYNOPSIS
   Function to Search and Delete given user from all site collections of the web application
  .DESCRIPTION
   This PowerShell function Scans each site collection under the all site collections of the given web application
   and deletes the user from site collections.
  .EXAMPLE
   DeleteUserFromAllSites "http://sharepoint.crescent.com" "global\davep" $true
   This example removes the user account "global\davep" from the web application "http://sharepoint.crescent.com"
  .INPUTS
   $WebAppURL - URL of the web application in which the user account to be scaned and or deleted
   $UserAccount - User account to delete from all sites
   $ScanOnly - Indicates whether the script should delete the user or only scan
  .OUTPUTS
   Writes the output on screen: List of site collections from where the user account is found/removed.
 #>

   
   #Get the web application
   $WebApp = Get-SPWebApplication $WebAppURL
   
   #Loop through each site collection
   foreach ($Site in $WebApp.Sites)
   {
    try
           {
      $ErrorActionPreference = "Stop"
      #Try to get the User
      $User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserAccount}
      
      #If user account found
      if($User -ne $null)
      {
       if($ScanOnly -eq $true)
       {
        Write-Host "Found user on: $($site.Rootweb.URL)"
       }
       else
       {
        #Remove the User from site collection
        $Site.RootWeb.SiteUsers.Remove($UserAccount)
        Write-Host "User Deleted from: $($site.Rootweb.URL)"
       }
      }
    
     }
    catch
           { 
      #Write error message on screen and to a LOG file
               write-host "Error Deleting user from site collection: $($site.rootweb.url)`n" $_.Exception.Message
      $_.Exception.Message >> "d:\error.log" 
           }
          finally
          {
              $ErrorActionPreference = "Continue"
                                $site.Dispose()
          }
   }
}

#Call the function
DeleteUserFromAllSites "http://sharepoint.crescent.com" "global\davep" $true 
This will delete SharePoint user from all site collections in the given web application.

To delete multiple users from all site collections, just store user accounts in an array and call the PowerShell function. E..g
#Array to store user accounts
$userNames= ("domain\user1", "me\salaudeena", "domain\user3")
#Iterate through the array
foreach($user in $userNames)
 {  
   #Call the function
   DeleteUserFromAllSites "http://sharepoint.crescent.com" $user $true 
 }

Remove user from SharePoint group:
From SharePoint Web interface, if you want to remove user from SharePoint group, simple navigate to the target group, select the user, click on Actions >> Choose "Remove users from Group"

Delete user from SharePoint group programmatically: If you want to remove user from group programmatically, This PowerShell script can help:
function RemoveUserFromGroup($SiteURL, $GroupName, $UserAccount)
{
 try
    {
     $ErrorActionPreference = "Stop"
  #Get the Web
  $web=Get-SPWeb $SiteURL
  #Get the User to Remove 
  $User  = Get-SPUser -Identity $UserAccount -Web $web

  #Get the Group by its name
  $Group = $Web.sitegroups | Where-Object {$_.Name -eq $GroupName}
  if($Group -ne $null)
  {
    $Group.RemoveUser($User)
    Write-Host "$($User) Removed from the Group: $($GroupName)"
  }
 }
 catch
    { 
   #Write error message on screen and to a LOG file
            write-host $_.Exception.Message
    }
 finally
    {
            $ErrorActionPreference = "Continue"
    }
}

#Call the function
RemoveUserFromGroup "http://sharepoint.crescent.com/sites/Operations" "Operations Members" "Corp\DaveP"

This will remove user from SharePoint group.

Viewing all articles
Browse latest Browse all 1058

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>