Question

Is there a way I can choose to remove a single user from all site collections within a single web application using powershell. There are number of user profiles whose domain account has been blocked or removed from the AD as such they need to be removed from the SharePoint sites as well.

I want a powershell script like this which takes two inputs :

removeuser.ps1 -webapplicationurl -domain\userid

On running this script , I should be able to delete the user (domain\userid) from all site collections with in the web application. Seeking someone to help me achieve this.

I have SharePoint 2010 and the solution I require needs to be 2010 compatible. Thanks in advance.

Was it helpful?

Solution

Can't test it myself but it wouldbe something like this:

######################## Start Variables ########################
$LoginName = "domain\login"
$siteURL = "http://SharePointSiteURL" #URL to any site in the web application.
######################## End Variables ########################
Clear-Host
$siteCount = 0
[system.reflection.assembly]::loadwithpartialname("Microsoft.SharePoint")
$site = new-object microsoft.sharepoint.spsite($siteURL)
$webApp = $site.webapplication
$allSites = $webApp.sites
foreach ($site in $allSites)
{

    $web = $site.openweb()
    $web.SiteUsers.Remove($LoginName)
    $web.Dispose()
    $siteCount++
}
$site.dispose()
write-host "Updated" $siteCount "Site Collections."
Licensed under: CC-BY-SA with attribution
Not affiliated with sharepoint.stackexchange
scroll top