A client needed a way to remove old domain users from their SharePoint 2010 farm. There was a company merger and they did not want the old domain users showing in the sites.
Enter the root site collection and the search parameter ex: domain and done.
** you might notice a table being created… I left this in here, but its not really used. The client had some additional requirements, that I have not included in this script.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | ## Reference to SharePoint DLL [void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint") ##Ask for WebApp Root url to enumerate or scope scan Write-Host "Please enter root url of WebApplication, ex: http://contoso" $siteurl = Read-Host "Value " Write-Host "Please enter the search parameter, ex: contoso " $searchP = Read-Host "Value " ##Create Table - ScanTable $ScanTable = New-Object system.Data.DataTable "ScanTable" $col1 = New-Object system.Data.DataColumn ("LoginName", [string]) $col2 = New-Object system.Data.DataColumn ("URL", [string]) $ScanTable.columns.add($col1) $ScanTable.columns.add($col2) ##Returning info for use in remainder of script $webapp = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($siteurl) ##Start looping through the sites collections foreach ($site in $webapp.Sites) { $spSite = new-object Microsoft.SharePoint.SPSite($site.url) $spWeb = $spSite.OpenWeb() ##Save file path, guid, and title of each closed webpart foreach ($_.LoginName in $spSite.RootWeb.SiteUsers | select LoginName) { if ($_.LoginName -like "*$searchP*") { $output = $ScanTable.Rows.Add($_.LoginName, $site.url) $spWeb.SiteUsers.Remove($_.LoginName) } } ##Clean Up $spSite.Dispose() $spWeb.Dispose() } ##Write txt file Write-Output $ScanTable | select URL | Sort-Object URL -Unique | Out-File C:users.txt -Append |