5

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)

Rosie O'Donnell
  • 179
  • 1
  • 7

2 Answers2

4

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
Mary Rivers
  • 798
  • 2
  • 9
  • 16
2

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/

Yuri Leontyev
  • 1,827
  • 12
  • 14
  • 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