I have tenant admin access and many users are using onedrive to share files and folders, we did some users blocked from signed in from O365 Admin.

I able to find all blocked users and onedrive url of blocked users using powershell. But i want to unshare / disable all files and folders which shared by them using powershell. currently manually i am doing this activity by opening one drive of one by one user and stop sharing of files and folders.

how can it be done using powershell?

Any help would be appreciated.


1 Answer 1


Here’s a sample PnP PowerShell script to remove all shared links for one user:

Function Remove-OneDriveSharingLink {
    param (   
    process {
        Connect-PnPOnline -Url $OneDriveURL 
        $Ctx= Get-PnPContext

        $Files= Get-PnPListItem -List "documents"
        foreach( $File in $Files) {       
            $Froles= $File.RoleAssignments

            If($Froles.Count -gt 0) {
                for ($i = $Froles.Count -1; $i -ge 0 ; --$i){   
                If($Link.title -like "SharingLinks*") {
                $Link = $null

Remove-OneDriveSharingLink -OneDriveURL "https://<tenant>-my.sharepoint.com/personal/<username>_<tenant>_onmicrosoft_com"

It doesn’t actually remove the links from Shared by you list, but it breaks all the links shared, internal or external.

Note: You need to have access to the user’s personal OneDrive or just use the user’s credential to avoid Access Denied error.

If you no longer need to share files using these blocked accounts, you can run the command below to disable external sharing features for them. It removes all external sharing links as well.

Set-SPOSite https://<tenant>-my.sharepoint.com/personal/<username>_<tenant>_onmicrosoft_com -SharingCapability Disabled


Remove External Shared links in OneDrive for Business using PowerShell.

Office 365 SharePoint/ODFB External User Sharing Management FAQs.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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