Wednesday, April 8, 2015

How to Remove invalid users from SharePoint Web Applications


Here is a PowerShell script to remove invalid users from the SharePoint Web Applications

 
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")

#Get all the Site Collections inside the Web Application
$SiteColl = Get-SPWebApplication "http://abc.xyz.com" | Get-SPSite -Limit All
 
#Define the List of Users
$Users =@('domain\abc',’domain\xyz’)

#Iterate through all the Site Collections
foreach($site in $SiteColl)
{
 #Iterate through users to Remove
 foreach($user in $Users)
 {
   #Check if User Exists in Site
   if ($Site.RootWeb.SiteUsers | Where {$_.LoginName -eq $User})
   {
      $Site.RootWeb.SiteUsers.Remove($User)
   Write-Host "$($user) has been Removed from the $($Site.RootWeb.URL)"
   }
  }
}

No comments:

Post a Comment