I'm trying to work out a process that would allow my helpdesk to delete an individual user's profile from Office 365 sharepoint online using powershell.

It's easy to connect to our Sharepoint Online URL and see the sites we have:

Connect-SPOService -url https://example-admin.sharepoint.com -Credential AccountWithOffice365AdminRights@example.com

I can verify that I'm connected. get-sposite returns a list of our sharepoint online URLs, as you expect.

Running commands such as repair-sposite or remove-sposite work for the entire site collection, e.g. repair-sposite -identity https://example-my.sharepoint.com works on the whole site collection, so the last thing I want to be doing is running remove-sposite at this level, and running it at a deeper level (repair-sposite -identity https://example-my.sharepoint.com/personal/user_example_com) fails to locate a sharepoint site to work with.

There are a few sites out there that have all cut and pasted the same snippet of code to iterate all the users in a sharepoint mysites collection and remove all of them, but I'm not convinced that code works for sharepoint 2013 sites and it's trivial to remove a user's permissions from a site. But I don't want to do either of those things, I just want to remove one user's profile from mysites.


I've been generating a library to use for sharepoint online, so I've been writing hundreds of these small tasks lately, but basically you'll need to use CSOM commands. first, download the sharepoint server 2013 client context SDK. Then make sure you include the DLL in your code:

Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'

Your path to the DLL may differ from mine.

Now you can use CSOM to do just about anything you want.

Here's some code that should accomplish what you're trying to do.

Import-Module 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll'  #Needed for CSOM
$SPOPassword="my plain text password"
$SPCred=New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($SPOUsername,(ConvertTo-SecureString $SPOPassword -AsPlainText -Force))
$context = New-Object Microsoft.SharePoint.Client.ClientContext("https://Blah.sharepoint.com/sites/.....")
$context.Credentials = $SPCred

$Web.SiteUsers|Select ID,Email,title #Display users and their IDs
$Web.SiteUsers.RemoveById(<ID of target user>)


Best I can find for you:


Which states:

There are no cmdlets available for managing SharePoint objects at a lower scope than the site collection.

But then gives this possibility:

There is however a workaround for manipulating objects in SharePoint Online at a lower level using the SharePoint Server 2013 Client Components SDK which enables remote development against SharePoint Server 2013.

But isn't what you are after for providing a simple PS script for the Helpdesk to use. You might consider opening a ticket with O365 support, but my experience with that has been hit or miss. Sometimes it seems I get someone that knows only scripted answers, and then there are times where it seems I get the actual developers themselves.

  • Having dug further into things, I don't think the components I need are exposed in the powershell cmdlets for Office 365 so while this isn't quite what I'm after, it's as good as I'm going to get. – Rob Moir Feb 7 '14 at 21:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.