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, you 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 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 easily! The following PowerShell script will remove the provided login account from your SharePoint Online site collections. Please note, that you must be a site collection administrator for all site collections of the Office 365 tenant before running this script. 

#Config Parameters
$UserAccount = ""
#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
        #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 ensure 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:

$TenantURL =  ""

#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
    Connect-PnPOnline -Url $_.URL -Credentials $Credential
    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

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

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


When a user leaves an organization or changes roles, it can be time-consuming to manually remove that user from all sites they have access to. This is where using SharePoint Online PowerShell comes in handy. By using SharePoint Online PowerShell to remove a user from all sites, You can ensure that your data is secure and that the user no longer has access to sensitive information. This process is quick, efficient, and automated, saving time and ensuring that the process is completed correctly.

Salaudeen Rajack

Salaudeen Rajack - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • This script is great… however microsoft throttle me after 5 mins with a 429 error 🙁

    • Yeah.. That’s the pain on bulk operations. Try to pause and re-execute. I believe the New-PnPBatch will support for such operations in the near future.

    • Delete 5,6 line.
      Replace 11 line by this : Connect-PnPOnline -Url $TenantAdminURL -UseWebLogin
      20: Connect-PnPOnline -Url $_.URL -UseWebLogin

  • Hi Sir,

    Is there any script to replace specific user on all SharePointOnline sites?



Leave a Reply

Your email address will not be published. Required fields are marked *