SharePoint Online: PowerShell to Remove User from All Sites

Requirement: Remove User from All Sites in SharePoint Online
SharePoint Online Delete User from All Sites using PowerShell

PowerShell to Remove a User from All SharePoint Online Site Collections:
Please note, you must be a site collection administrator for all site collections before running this script. 
#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount = "Jacob@crescent.com"
 
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential (Get-Credential)
 
#Get all Site Collections
$SitesCollections = Get-SPOSite -Limit ALL

#Iterate through each site collection
ForEach($Site in $SitesCollections)
{
    Write-host -f Yellow "Checking Site Collection:"$Site.URL
 
    #Get the user from site collection
    $User = Get-SPOUser -Limit All –Site $Site.URL | Where {$_.LoginName -eq $UserAccount}
 
    #Remove the User from site collection
    If($User)
    {
        #Remove the user from the site collection
        Remove-SPOUser -Site $Site.URL –LoginName $UserAccount
        Write-host -f Green "`tUser $($UserAccount) has been removed from Site collection!"
    }
}

Related Posts:
SharePoint Online: PowerShell to Remove User from All Sites SharePoint Online: PowerShell to Remove User from All Sites Reviewed by Salaudeen Rajack on July 15, 2018 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.