Find and Remove SharePoint User Accounts

By James|07/23/2017|,

Introduction

If you want to find and remove SharePoint user accounts, this can either be a trivial task or a rather cumbersome endeavor.  If your portal only has one site collection and a small number of users, the GUI is probably the easiest method.  However, if your portal contains multiple site collections and thousands of users, finding and removing them with PowerShell will be much easier and faster.

Find SharePoint User Accounts

To locate a user account with the GUI, navigate to the user information list page.  This can be accessed via http://<portal>/_catalogs/users/detail.aspx or http://<portal>/_catalogs/users/simple.aspx.

Here's a small script that will enumerate all portal site collections and return any matches of a user account based upon their e-mail address:

#-------------------------------------------------------------
# Title:       QuerySPUIL.ps1
# Version:     1.2, 06OCT17
# Author:      James Sanders
# Description: Query all site collection user lists for a user
#-------------------------------------------------------------

# The GUI Way
# http://<portal>/_catalogs/users/detail.aspx
# http://<portal>/_catalogs/users/simple.aspx

# Script requires user e-mail for query
Param(
  [Parameter(Mandatory=$True)]
  [string]$userEmail
)

$matchesFound = 0
$siteCount = 0

Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Write-Host
   
ForEach ($Site in $(Get-SPSite -Limit All | ? {!$_.Url.Contains("Office_Viewing_Service_Cache")})){
  $RootWeb = $Site.RootWeb
  Write-Host "Querying site collection : $($Site.Url)"
  $userQuery = $RootWeb.SiteUsers | Where {$_.email -eq $userEmail}
  If ($userQuery) {
    $UIL = $RootWeb.SiteUserInfoList
    Write-Host "Found the following user(s):"
    ForEach ($user in $userQuery) {
      $userItem = $UIL.Items.GetItemById($user.ID)
      Write-Host "User Name: " $user.DisplayName
      Write-Host "Login ID:  " $user.UserLogin
      Write-Host "Created :  " $userItem['Created']
      $matchesFound++
    }
  }
  Else { Write-Host "User does not exist in this site collection" }
  $RootWeb.Dispose()
  $Site.Dispose()
  Write-Host ""
  $siteCount++
}

Write-Host "User $userEmail found $matchesFound times across $siteCount site collections"

Remove SharePoint User Accounts

The process to remove a user account with the GUI is the same as finding a user.  Navigate to the user information list page.  This can be accessed via http://<portal>/_catalogs/users/detail.aspx or http://<portal>/_catalogs/users/simple.aspx.  You can then page through the list, find the user to remove, select the user and finally delete them from the site collection.

Here's a small script that will remove a user account from all portal site collections based upon their login ID:

#-------------------------------------------------------------
# Title:       RemoveUserFromPortal.ps1
# Version:     1.1, 04OCT17
# Author:      James Sanders
# Description: Remove user from all portal site collections
#-------------------------------------------------------------

# Script requires user login for query
Param(
  [Parameter(Mandatory=$True)]
  [string]$userLogin
)

$timeStarted = Get-Date

Add-PSSnapin Microsoft.SharePoint.PowerShell -EA SilentlyContinue
Write-Host

ForEach ($site in (Get-SPSite -Limit All)) {
  Write-Host "Processing site: " $site.Url
  $user = $site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $userLogin}
  If ($user) {
    Write-Host "  User located in site collection, removing ... " -NoNewline
    $web = $site.RootWeb
    Remove-SPUser $userLogin -Web $web.Url -ErrorAction SilentlyContinue -Confirm:$False
    Write-Host "Done"
    $web.Dispose()
  }
  Else {
    Write-Host "  User does not exist in site collection"
  }
  $site.Dispose()
}

#Finalize script statistics
$timeFinished = Get-Date
$timeDuration = ("{0:hh\:mm\:ss}" -f ($timeFinished - $timeStarted)).Substring(0,12)
Write-Host "Script started:  $timeStarted"
Write-Host "Script finished: $timeFinished"
Write-Host "Script duration: $timeDuration"

Additional Resources

For even more fun with SharePoint user accounts check out these additional articles:

Find Duplicate Users in SharePoint User Information List

Update SharePoint User Information List

Copyright 2011 - 2024 The Lazy IT Admin | All Rights Reserved
menu-circlecross-circle linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram