Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
Function DeleteUserFromAllSites([string]$WebAppURL, [string]$UserAccount, [bool]$ScanOnly)
{
#Get the web application
$WebApp = Get-SPWebApplication $WebAppURL
#Loop through each site collection
foreach ($Site in $WebApp.Sites)
{
try
{
$ErrorActionPreference = "Stop"
#Try to get the User
$User = $Site.RootWeb.SiteUsers | Where-Object {$_.LoginName -eq $UserAccount}
#If user account found
if($User -ne $null)
{
if($ScanOnly -eq $true)
{
Write-Host "Found user on: $($site.Rootweb.URL)"
}
else
{
#Remove the User from site collection
$Site.RootWeb.SiteUsers.Remove($UserAccount)
Write-Host "User Deleted from: $($site.Rootweb.URL)"
}
}
}
catch
{
#Write error message on screen and to a LOG file
write-host "Error Deleting user from site collection: $($site.rootweb.url)`n" $_.Exception.Message
$_.Exception.Message >> "d:\error.log"
}
finally
{
$ErrorActionPreference = "Continue"
$site.Dispose()
}
}
}
#Call the function
DeleteUserFromAllSites "http://sharepoint.crescent.com" "global\davep" $true
No comments:
Post a Comment