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

A user has left the company and I been finding her many different places from main portal http://portal.inhouse.ca to all the site collections, sub sites and SP groups..

Is there a powershell way to delete her from everywhere? (domain\jsmith)

share|improve this question

2 Answers 2

up vote 2 down vote accepted

Please see the script below, it "Remove / Delete users from all sites and site collections within a web application.": http://blog.isaacblum.com/2011/02/24/remove-delete-users-from-all-sites-and-site-collections-within-a-web-application/

share|improve this answer
    
Thanks for the ready-made code... –  Rosie O'Donnell Nov 14 '11 at 20:45
    
No problem, all common tasks are already implemented by someone and blogged:) You just need to know what to solution is right and use it. –  Yuri Leontyev Nov 14 '11 at 22:03

To loop through all the sites in a portal use foreach. sample below

$portalUrl = Read-Host "Enter Site URL" 
$rootSite = New-Object Microsoft.SharePoint.SPSite($portalUrl)
$spWebApp = $rootSite.WebApplication 

foreach($site in $spWebApp.Sites)
{

}

Implement above foreach code for below if you need to go through each web in a portal.

function RemoveUser([string]$Site, [string]$SiteCollection, [string]$User) {

    # GAC

    [System.Reflection.Assembly]::LoadWithPartialName("System.Web") | Out-Null
    [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") | Out-Null

    # Connect To Sharepoint

    $SPSite = New-Object Microsoft.SharePoint.SPSite($Site)
    $OpenWeb = $SPSite.OpenWeb($SiteCollection)

    # Check if User Exists in Site

    if ($OpenWeb.SiteUsers | Where {$_.LoginName -eq $User}) {
        $User = $OpenWeb.SiteUsers | Where {$_.LoginName -eq $User}
        $OpenWeb.SiteUsers.Remove($User)

        Write-Host "User: $User Successfully Removed from Site: $Site/$SiteCollection"
    } else {
    Write-Host "User: $User Does not exist on Site: $Site/$SiteCollection"
    }
    $SPSite.Dispose()
    $OpenWeb.Dispose()
}

Usage:

PS > Remove-SPUser.ps1 -Site http://wss -SiteCollection Test -User Domain\User

PS > Remove-SPUser.ps1 -help
share|improve this answer
    
Yours is good too. Almost exactly same as what default suggested. Thanks –  Rosie O'Donnell Nov 14 '11 at 19:45

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.