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:

Do you need to remove a user from all sites and groups? Well, This blog post is to guide you on how to remove a user from all sites in SharePoint Online using PowerShell. If you are a SharePoint administrator, there may come a time when you need to remove a user from all of your SharePoint Online sites. Maybe when you know the user will not be returning, the person has left the company, have to disable a user temporarily, if an account is hacked, or you are cleaning up user permissions and no longer need a user to have access to any sites. Whatever the reason, this PowerShell script will help you to quickly and easily remove users from all of your SharePoint Online sites.

Although you can use the Web browser interface to remove a user from a site in SharePoint Online, the PowerShell approach is more efficient if your goal is just to delete them from everywhere fast and easy! The following PowerShell script will remove the provided login account from your SharePoint Online site collections. Please note, you must be a site collection administrator for all site collections of the Office 365 tenant before running this script. 

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount = "[email protected]"
 
#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!"
    }
}

This will assure that the user no longer has access to any content or sites when they are removed.

Delete a User from All SharePoint Online Sites using PnP PowerShell

At times, it’s necessary to remove a user account from all SharePoint Online sites to ensure that the specific user has no access to the organization’s data. Let me show you how to remove a user account from all of your SharePoint Online sites with PnP PowerShell:

#Parameters
$TenantURL =  "https://crescent.sharepoint.com"
$UserID="i:0#.f|membership|[email protected]"

#Get Credentials to connect
$Credential = Get-Credential

#Frame Tenant Admin URL from Tenant URL
$TenantAdminURL = $TenantURL.Insert($TenantURL.IndexOf("."),"-admin")
#Connect to PnP Online
Connect-PnPOnline -Url $TenantAdminURL -Credentials $Credential

#Get All Site collections - Filter BOT and MySite Host
$Sites = Get-PnPTenantSite -Filter "Url -like '$TenantURL'"

#Iterate through all sites
$Sites | ForEach-Object { 
    Write-host "Searching in Site Collection:"$_.URL -f Yellow
    #Connect to each site collection
    $SiteConn = Connect-PnPOnline -Url $_.URL -Credentials $Credential -ReturnConnection
    If((Get-PnPUser | Where {$_.LoginName -eq $UserID}) -ne $NULL)
    {
        #Remove user from site collection
        Remove-PnPUser -Identity $UserID -Confirm:$false
        Write-host "`tRemoved the User from Site:"$_.URL -f Green
    }
    Disconnect-PnPOnline -Connection $SiteConn
} 

This PnP PowerShell removes the user from all SharePoint Online sites in the tenant.

Here is another post on how to Remove External User from SharePoint Online with PowerShell – SharePoint Online: Remove External Users using PowerShell

Salaudeen Rajack

Salaudeen Rajack is a SharePoint Architect with Two decades of SharePoint Experience. He loves sharing his knowledge and experiences with the SharePoint community, through his real-world articles!

4 thoughts on “SharePoint Online: PowerShell to Remove User from All Sites

Leave a Reply