Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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.

share|improve this question
    
is there a way to make an output with the results for this? just a text file would work. –  user17313 May 29 '13 at 15:24
add comment

1 Answer

up vote 4 down vote accepted

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."
share|improve this answer
1  
Works like a charm ! Thanks a lot Falak. –  Arko D May 17 '12 at 13:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.