SharePoint Online: Remove User from Site Collection using PowerShell

Requirement: SharePoint Online Remove User from a site collection.

How to delete a user from the site collection in SharePoint Online?

As a SharePoint site owner, you may, at some point, need to delete a user from a site collection for various reasons. For example, the user may no longer need access to the site, or you may want to remove an inactive user. Whatever the reason, you can easily delete a user from a SharePoint Online site collection. If you need to delete a user from your site collection, there are a couple of different ways to do it. This blog post will show you how to use PowerShell and the SharePoint Web interface with a browser to delete users.

To remove a user from the SharePoint Online site collection, follow the below steps:

  1. Login to SharePoint Online site as Administrator >> Navigate to “All People” page by appending: /_layouts/15/people.aspx?membershipGroupId=0 to the URL (E.g., https://crescent.sharepoint.com/_layouts/15/people.aspx?membershipGroupId=0)
  2. On the “All People” page, Select the user you want to delete >> Click on the “Actions” menu from the command bar >> Delete Users from Site Collection.
    sharepoint online Remove user from site collection PowerShell
  3. Confirm the prompt to remove the user from the site collection. This deletes the user from the entire site collection, including from all groups where the particular user was a member!

Deleting a user from your site collection can be a helpful way to clean up your site and remove any unwanted users. However, if you delete a user, all their data will stay intact in the site collection – Except the “Created By” and “Modified By”, and people Picker field values will be lost as it removes the user from the user information list as well.

Now, let’s remove user permissions in SharePoint Online using PowerShell!

SharePoint Online: Remove user from site collection using PowerShell

If you are a site collection administrator for a SharePoint Online environment and need to remove a user from the site collection, PowerShell can help! To remove a user or security group from the site collection in SharePoint Online, we can use the Remove-SPOUser PowerShell cmdlet from SharePoint Online Management Shell. Here is the syntax for the Remove-SPOUser cmdlet:

Remove-SPOUser
      [-Group <String>]
      -LoginName <String>
      -Site <SpoSitePipeBind>
      [<CommonParameters>]

Here is an example of how to remove a user from a site collection:

Remove-SPOUser https://crescent.sharepoint.com -LoginName zahia@Crescent.com

This cmdlet can be used to remove both an internal and an external user from a SharePoint site collection. Let’s make it a complete script. First, connect to your SharePoint Online tenant using the PowerShell Connect-SPOService cmdlet. You will need to have an account with SharePoint Online administrator privileges to do this. Once connected, you can use the Remove-SPOUser command to delete the user from the site where the user is located. This command takes two parameters: The SiteUrl parameter specifies the URL of the site collection, and the LoginName parameter specifies the login name of the user you want to delete.

#Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com/sites/warehouse"
$UserID="steve@Crescent.com"
  
#Connect to SharePoint Online Tenant Admin
Connect-SPOService -URL $AdminSiteURL -Credential (Get-Credential)

# Check if the user still exists
$User = Get-SPOUser -site $SiteURL | Where {$_.LoginName -eq $UserID}

If($User)
{
    #sharepoint online powershell remove user permissions from site collection
    Remove-SPOUser -Site $SiteURL -LoginName $UserID
    Write-host -f Green "Removed User $UserID from the site!"
}
Else
{
    Write-host -f Yellow "User doesn't exist on the site!"
}

Please note that you must be a SharePoint Online administrator or Global Administrator with site collection administrator rights to run the Remove-SPOUser cmdlet.

Follow the above PowerShell cmdlets for any other users you need to delete from SharePoint Online site collections. Windows PowerShell ISE can also be used to execute the script.

powershell to remove user from site collection in sharepoint online

Remove a user from SharePoint Online using PowerShell

As an administrator of a SharePoint Online environment, there may be times when you need to remove user permissions. Here is the CSOM PowerShell script to delete a user from the site:

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
 
#Set parameter values
$SiteURL="https://Crescent.sharepoint.com/"
$UserID="peter@crescent.com"

Try {
    #Get Credentials to connect
    $Cred= Get-Credential
    $Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
  
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Credentials
 
    #Get the web
    $Web=$Ctx.Web
    $Ctx.Load($Web)
    $Ctx.ExecuteQuery()

    #Frame Login Name
    $LoginName = "i:0#.f|membership|"+$UserID

    #Get the User to Delete
    $User = $Web.SiteUsers.GetByLoginName($LoginName)
    $Ctx.ExecuteQuery()

    If($User -ne $null)
    {
        #remove user from sharepoint online powershell
        $Ctx.Web.SiteUsers.RemoveByLoginName($LoginName)
        $Ctx.ExecuteQuery()
 
        Write-Host "User: '$UserID' has been Removed from the site Successfully!" -ForegroundColor Green  
    }
}
Catch {
    write-host -f Red "Error Removing User from Site!" $_.Exception.Message
}

Delete User from Site Collection in SharePoint Online using PnP PowerShell

Are you looking for a way to remove a user from your SharePoint Online site collection using PowerShell? Here is how to delete a user from the SharePoint Online site with PnP PowerShell:

#Config Variables
$SiteURL = "https://Crescent.sharepoint.com/sites/marketing"
$UserID="i:0#.f|membership|John@Crescent.com"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -Credentials (Get-Credential)

#sharepoint online powershell delete user from site collection
Remove-PnPUser -Identity $UserID -Force

This removes the given user by its UPN from all SharePoint Groups and direct permissions from a site collection, including “Site collection Administrators”. Please note that this does not affect the AD groups on the site where that specific user is a member!

Wrapping up

And that wraps up the process for removing a user from a SharePoint Online site collection through PowerShell. While the SharePoint site settings page provides a graphical interface for managing users and groups, PowerShell grants the ability to automate and streamline user administration tasks. With just a few PowerShell cmdlets, site admins can easily look up the desired site collection and user, and detach the user’s permissions. Useful for offboarding scenarios or restricting access, these PowerShell scripts simplify user removal.

To remove a user from all SharePoint Online sites, use: SharePoint Online: PowerShell to Remove User from All Sites

How do I delete a collection admin in SharePoint online?

To remove a site collection admin, You can use the SharePoint Admin Center or the site settings page of the site collection. Deleting a site collection admin using the PowerShell cmdlet Set-SPOUser or the CSOM PowerShell script and PnP PowerShell cmdlet “Remove-PnPSiteCollectionAdmin” is also possible.
More info: Remove site collection administrator using PowerShell in SharePoint Online

How do I delete a SharePoint site from Office 365?

You can delete a SharePoint site from the SharePoint Online Admin Center: Click on Sites >> Active Sites. From the site collections list, select the site collection to delete. Click on the Delete button in the command bar. You can also delete a site by going to the site >> Click on the settings icon >> Site Information link >> Click on the Delete Site link! With PowerShell, Remove-PnPTenantSite, and Remove-SPOSite cmdlets are the other possible ways to remove a site from your Office 365 environment. More info: How to delete a SharePoint Online site?

How do I delete an external user in SharePoint online?

To remove an external user, navigate to the “All People” view by appending: /_layouts/15/people.aspx?MembershipGroupId=0, select the checkbox next to the user, and click Actions >> Delete Users from Site Collection. Use the “Remove-SPOExternalUser” cmdlet from SharePoint Online Management Shell or Remove-PnPExternalUser in PnP PowerShell to remove external users from SharePoint Online sites.
More info: Remove External user from SharePoint Online

Salaudeen Rajack

Salaudeen Rajack - Information Technology Expert with Two-decades of hands-on experience, specializing in SharePoint, PowerShell, Microsoft 365, and related products. He has held various positions including SharePoint Architect, Administrator, Developer and consultant, has helped many organizations to implement and optimize SharePoint solutions. Known for his deep technical expertise, He's passionate about sharing the knowledge and insights to help others, through the real-world articles!

5 thoughts on “SharePoint Online: Remove User from Site Collection using PowerShell

  • Is there a simple way to modify the PowerShell script so that it only keeps the site collection owners and/or the M365 group owners?

    Reply
  • First, thank you.
    Second, at this part:
    …all their data will stay intact in the site collection – Except the “Created By” and “Modified By”, and people Picker field values will be lost as it removes the user from the user information list as well.

    Currently those fields continue to store the information when you delete the user from “AD” and “All People”.
    When will that information be lost?
    or
    Is it an improvement implemented?

    Regards.

    Reply
  • I should be
    /_layouts/15/people.aspx?membershipGroupId=0
    Not
    /_layouts/15/people.aspx/membershipGroupId=0

    Reply
  • It helped..!! Thank you so much..!!

    Reply

Leave a Reply

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