Share via

query on onedrive

Roger Roger 6,406 Reputation points
Nov 19, 2024, 9:50 PM

Hi All,

I have a user, user1@contoso.com, and I need to determine how many users currently have access to user1's OneDrive. If user1 has shared any files or folders with other users, I want to revoke all their access. Please guide me how to achieve this.

Additionally, I need to grant user2@contoso.com access to user1's OneDrive, which I can achieve using the following syntax:

Set-SPOUser -Site https://contoso-my.sharepoint.com/personal/user1_contoso_com -LoginName user2@contoso.com -IsSiteCollectionAdmin $true
Microsoft Exchange Online
OneDrive
OneDrive
A Microsoft file hosting and synchronization service.
1,166 questions
SharePoint
SharePoint
A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.
10,904 questions
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,612 questions
OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,287 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Yanli Jiang - MSFT 27,321 Reputation points Microsoft Vendor
    Nov 20, 2024, 8:46 AM

    Hi @Roger Roger ,

    Welcome to Q&A forum!

    If you want to get how many users have access to User 1's OneDrive:

    First, you need to add yourself as an administrator of User 1's OneDrive.

    https://www.sharepointdiary.com/2017/04/gain-admin-permission-to-onedrive-for-business-sites-using-powershell.html

    Non-official, just for reference.

    Then, you can get the Sharing report in User 1's OneDrive.

    Open User1's OneDrive, go to OneDrive settings--More Settings--Run sharing report, and you'll be asked where in the drive you want to save the report. The resulting csv file shows the different ways a file has been shared and with whom.

    https://www.ytria.com/blog/onedrive-sharing-report-for-all-users/

    Non-official, just for reference.

    Finally, if you want to terminate sharing in batches, you can use PowerShell to remove the sharing link.

    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 breaks all the links shared, internal or external.

    For more info., please see:

    https://sharepoint.stackexchange.com/questions/278316/how-can-unshare-stop-sharing-of-onedrive-files-and-folders-using-powershell

    The PowerShell command you use to grant User 2 access to User 1's OneDrive is completely correct. The premise is that you have administrator privileges to User 1's OneDrive.

    For your references:

    https://learn.microsoft.com/en-us/microsoft-365/admin/add-users/remove-former-employee-step-5?view=o365-worldwide#access-a-former-users-onedrive-documents

    https://techcommunity.microsoft.com/discussions/onedriveforbusiness/onedrive-external-sharing-and-departed-users/130931

    Good day!


    If the answer is helpful, please click "Accept as Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.