SharePoint Online: Get OneDrive URL of a User using PowerShell

Requirement: PowerShell to Get OneDrive URL in SharePoint Online

How to Get OneDrive URL of a User?
Typically, OneDrive sites are located at the URL: https://tenant-my.sharepoint.com/personal/upn  (E.g. https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com), To get a SharePoint Online user's OneDrive site, follow these steps:
  • Login to SharePoint Online Admin Center
  • Click on "User Profiles" link from left navigation >> Click on "Manage User Profiles" link under "People" group
    change onedrive site collection administrator
  • Search and pick the user profile which you want to gain access. From the search result, Click on the menu item "Manage Personal Site" from the context menu.
    SharePoint Online PowerShell to Get OneDrive URL
This takes you to the OneDrive site!

SharePoint Online: PowerShell to Get OneDrive URL
Let's query user profile to retrieve Personal site URL. Remember to set the user name and admin site URL parameters prior running the script.
#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
Add-Type -Path "C:\Program Files\Common Files\microsoft shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.UserProfiles.dll"

#Set Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount="salaudeen@crescent.com"

Try {
    #Setup Credentials to connect
    $Cred= Get-Credential
 
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)
         
    #Get the User
    $User = $Ctx.web.EnsureUser($UserAccount)
    $Ctx.Load($User)
    $Ctx.ExecuteQuery()
 
    #Get OneDrive Site URL from User Profile
    $PeopleManager = New-Object Microsoft.SharePoint.Client.UserProfiles.PeopleManager($Ctx)
    $UserProfile = $PeopleManager.GetPropertiesFor($User.LoginName)
    $Ctx.Load($UserProfile)
    $Ctx.ExecuteQuery()

    #Get the User Profile Property
    Write-host $UserProfile.PersonalUrl
 
}
Catch {
    write-host -f Red "Error Getting User Profile Properties!" $_.Exception.Message
}
This script retrieves OneDrive (MySite) URL of a given user. E.g. https://crescent-my.sharepoint.com/personal/salaudeen_crescent_com/

PnP PowerShell to Get OneDrive URL of a User:
Similarly, you can query user profile and get My site URL with PnP PowerShell as,
#Set Config Parameters
$AdminSiteURL="https://crescent-admin.sharepoint.com"
$UserAccount="salaudeen@crescent.com"

#Connect to the site
Connect-PnPOnline $AdminSiteURL -Credentials (Get-Credential)

#Get OneDrive URL of the User
Get-PnPUserProfileProperty -Account $UserAccount | Select PersonalUrl
Here is another post to get all OneDrive sites collections of the Tenant: Find All OneDrive Site Collections in SharePoint Online using PowerShell
SharePoint Online: Get OneDrive URL of a User using PowerShell SharePoint Online: Get OneDrive URL of a User using PowerShell Reviewed by Salaudeen Rajack on January 25, 2019 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.