0

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.

Thanks

1

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

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

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

            If($Froles.Count -gt 0) {
                for ($i = $Froles.Count -1; $i -ge 0 ; --$i){   
                $Link=$Froles[$i].Member
                $Ctx.Load($Link)
                $Ctx.ExecuteQuery()
                If($Link.title -like "SharingLinks*") {
                    $Froles[$i].DeleteObject()
                }
                $Link = $null
            }
            $Ctx.ExecuteQuery()           
            }      
        }
    }
}

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

References:

Remove External Shared links in OneDrive for Business using PowerShell.

Office 365 SharePoint/ODFB External User Sharing Management FAQs.

1

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.