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 from the site collection in SharePoint Online, we can use the Remove-SPOUser PowerShell cmdlet from SharePoint Online Management Shell. Here is an example:

Remove-SPOUser https://crescent.sharepoint.com -LoginName [email protected]

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.

#Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com/sites/ops"
$UserID="[email protected]"

#Get Credentials to connect
$Cred = Get-Credential 

#Connect to SharePoint Online Admin Site
Connect-SPOService -Url $AdminSiteURL -Credential $cred 

#sharepoint online powershell remove user permissions from site collection
Remove-SPOUser -Site $SiteURL -LoginName $UserID

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="[email protected]"

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|[email protected]"

#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, This does not affect the AD groups in the site where that specific user is a member!

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. It’s also possible to delete a site collection admin using the PowerShell cmdlet Set-SPOUser or the CSOM PowerShell script and PnP PowerShell cmdlet “Remove-PnPSiteCollectionAdmin”.
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 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 Settings icon >> Site Information link >> Click on the Delete Site link! With PowerShell, Remove-PnPTenantSite and Remove-SPOSite cmdlet is the other possible ways to remove a site from your Office 365 environment. Deleted SharePoint sites go to the recycle bin.
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 - SharePoint Expert with Two decades of SharePoint Experience. Love to Share my knowledge and experience with the SharePoint community, through real-time articles!

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

  • 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 *