Get User OneDrive Drive ID

MattGlomski-3549 20 Reputation points
2023-06-06T19:53:25.4366667+00:00

Is there a way to get the OneDrive Drive ID of a User using PowerShell Microsoft.Graph.Files module?

I have tried:

Get-MgUser -userid {UserPrincipalName / Id} -property Drive | Select-object -expandproperty Drive

The results I get are:

Id Name DriveType CreatedDateTime


The goal is to write a script that can list all items within a OneDrive library that are shared to external users.

Thank you,

Microsoft 365 and Office | SharePoint | For business | Windows
Windows for business | Windows Server | User experience | PowerShell
Microsoft Security | Microsoft Graph
{count} votes

Accepted answer
  1. CarlZhao-MSFT 46,376 Reputation points
    2023-06-08T03:18:12.1333333+00:00

    Hi @Matt Glomski

    You have multiple ways to get a user's drive id.

    Using Microsoft Graph PowerShell:

    Import-Module Microsoft.Graph.Files
    Connect-MgGraph -Scopes "Files.ReadWrite.All"
    $userId = 'xxxxxxxxxxxxxxxxxx'
    Get-MgUserDefaultDrive -UserId $userId
    

    Using Azure AD PowerShell:

      $clientID = 'xxxxxxxxxxx'     
      $secretKey = 'xxxxxxxxxxxx'    
      $tenantID = 'xxxxxxxxxxxxx'                 
      $authUrl = "https://login.microsoftonline.com/" + $tenantID + "/oauth2/v2.0/token/"    
      $body = @{     
          "scope" = "https://graph.microsoft.com/.default";     
          "grant_type" = "client_credentials";     
          "client_id" = $ClientID     
          "client_secret" = $secretKey     
      }
            
      $authToken = Invoke-RestMethod -Uri $authUrl –Method POST -Body $body                 
      $url = "https://graph.microsoft.com/v1.0/users/xxxxxxxx/drive"   
      $headers = @{     
      "Authorization" = "Bearer $($authToken.access_token)"
       "Content-type"  = "application/json"     
      }     
      $response = Invoke-RestMethod -Uri $url -Headers $headers -Method Get
    
      Write-Host $response.id
    

    User's image

    Hope this helps.

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

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Limitless Technology 44,766 Reputation points
    2023-06-07T12:09:09.3966667+00:00

    Hello there,

    You could just use the Get Drive API.

    https://learn.microsoft.com/en-us/onedrive/developer/rest-api/api/drive_get?view=odsp-graph-online

    You can query the user profile and get My site URL with PnP PowerShell as,

    #Set Config Parameters

    $AdminSiteURL="https://crescent-admin.sharepoint.com"

    $UserAccount="******@crescent.com"#UPN

    #Connect to the site

    Connect-PnPOnline $AdminSiteURL -Interactive

    #powershell to get onedrive for business url

    Get-PnPUserProfileProperty

    Hope this resolves your Query !!

    --If the reply is helpful, please Upvote and Accept it as an answer--


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.