One of the great abilities of OneDrive for Business and SharePoint Online is the ability to quickly and easily share a link to a file with people outside your organisation.
I’m not going to show you how to do this in this article but if you need to see how this is done have a look at:
Share OneDrive files and folders
In the above example you’ll see that the file Employee Engagement Plan.docx has been been shared by the owner with an external user (Lewis Collins).
Somewhere else, you can see that this user (Lewis Collins) has the document open to work on.
One of the benefits of sharing using OneDrive for Business is that the sharing rights can easily be revoked by the original user if desired.
The easiest way to achieve this would be simply to hit the cross next to the external users name in the web interface as shown above.
The original user would then be prompted the Remove the external user, which they would do to remove access.
That all works fine when you have a small number of shared files and a small number of users working with a file. It becomes a lot more problematic when you start scaling to many users as you can see here:
What happens when you need to find just one user to remove sharing from amongst a list of hundreds of users?
PowerShell to the rescue!
1. Connect to SharePoint Online via PowerShell. You can use my script at:
to do this. You’ll need to know the tenant name prior i.e. the part before the .onmicrosoft.com (e.g. mydomain.onmicrosoft.com)
2. Run the PowerShell command:
Get-spouser -site https://<mydomain>-my.sharepoint.com/personal/<user>_<mydomain>_onmicrosoft_com -limit all
to display of all the users who have access to the specific OneDrive for Business site.
In this list you should find your external user in the format of:
This may vary slight but you should also be able to identify the user by their Display name if needed.
3. Run the PowerShell command:
remove-spouser –site https://<mydomain>-my.sharepoint.com/personal/<user>_<mydomain>_onmicrosoft_com -loginname <firstname.lastname@example.org>#ext#<mydomain>.onmicrosoft.com
4. Run a sharing report
In the source OneDrive for Business, select the COG in the upper right corner and then the option OneDrive Settings.
Now select More Settings on the left and Run sharing report from the options that appear on the right as shown above.
Nominate a folder for this report to be sent to.
You’ll receive an email when the report is ready. It will be in Excel format as you can see above.
Open the file and do a search for the external email address of the removed users.
The removed users should not appear in the report as expected.
If you now look at the sharing option for file(s) in that OneDrive for Business you should find that the removed user no longer appears, as shown above.
If the external user, who has just been removed, actually has the file open at the moment that access is removed they will see:
and be prompted to Reconnect. If they then attempt to reconnect they will see:
and will be denied access going forward.
Note – This removes that users access to ALL files shared in the OneDrive for Business location, not just for a single file.
I also have another freely available script at:
that will display a list of all externally shared across your tenant as shown above.
If you do have a situation where you have large numbers of shared files or shared users in OneDrive for Business and you wish to make bulk removal easier, I’d encourage you to look at PowerShell as an option. However, remember, this option will remove ALL sharing for that user(s) across the WHOLE OneDrive for Business.
3 thoughts on “Viewing and removing OneDrive for Business Sharing with PowerShell”
The onmicrosoft_com does not work for me, needs to be UPN probably, using domain_ext does the trick
Is there any way to remove the one particular external user from the Sharing Link for individual file using PowerShell? not from entire site collection.
Yes but you’d need to use PowerShell PnP for this