SharePoint Online: Remove User from Site Collection using PowerShell

Requirement: SharePoint Online Remove User from a site collection.

How to Delete a user from site collection in SharePoint Online?

If you need to delete a user from your site collection in SharePoint Online, there are a couple of different ways to do it. In this blog post, we’ll show you how to use PowerShell and the GUI methods to delete users.

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

  • 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)
  • On the “All People” page, Select the user you want to delete >> Click on “Actions” Menu >> Delete Users from Site Collection
    sharepoint online Remove user from site collection PowerShell
  • 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 of!

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 of their data will stay intact in the site collection – Except the “Created By”, “Modified By”, and people Picker field values lost their values.

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 site collection in SharePoint Online, we can use the Remove-SPOUser PowerShell cmdlet. 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:

#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
powershell to remove user from site collection in sharepoint online

Remove 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

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!

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