kwizcom banner advertisement

SharePoint Online: Remove User from Site Collection using PowerShell

Requirement: SharePoint Online Remove User from site collection

How to Delete user from site collection in SharePoint Online?
To remove a user from 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)
  • In 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 user from site collection. This deletes the user from entire site collection, including from all groups where the particular user was member of!

SharePoint Online: Remove user from site collection using PowerShell
To remove a user from site collection in SharePoint Online, we can use "Remove-SPOUser"  PowerShell cmdlet. Here is an example:
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. Lets make it a complete script:
#Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$SiteURL="https://crescent.sharepoint.com/sites/ops"
$UserID="zahia@crescent.com"

#Get Credentials to connect
$Cred = Get-Credential 

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

#Remove user 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
Here is the CSOM PowerShell script to delete user from 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://crescenttech.sharepoint.com/"
$UserID="peter@thecrescenttech.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 the user from site
        $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
}

To add new user to a group in SharePoint online, use: How to Add User to a Group in SharePoint Online using PowerShell
SharePoint Online: Remove User from Site Collection using PowerShell SharePoint Online: Remove User from Site Collection using PowerShell Reviewed by Salaudeen Rajack on August 23, 2016 Rating: 5

1 comment:

Please Login and comment to get your questions answered!

Powered by Blogger.