Entra / Microsoft 365 · SharePoint & OneDrive
Provision OneDrive accounts
A script to pre-provision OneDrive for Business accounts.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes Directory.Read.All -NoWelcome
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
Try {Connect-MgGraph -Scopes Directory.Read.All -NoWelcome} Catch {Write-Error "Failed to connect to Microsoft Graph: $_"exit}# This part is to generate a hash table of OneDrive sites that we can use to look upImport-Module Microsoft.Online.SharePoint.PowerShell -UseWindowsPowerShell -SkipEditionCheck# Make sure that you use the right URL for your tenant hereConnect-SPOService -Url https://office365itpros-admin.sharepoint.comWrite-Host "Looking for existing OneDrive sites..."[array]$OneDriveSites = Get-SPOSite -IncludePersonalSite $true -Limit All `-Filter "Url -like '-my.sharepoint.com/personal/'" | Select-Object Owner, URL# Build the hash table$OneDriveHashTable = @{}ForEach ($Site in $OneDriveSites) {[string]$SiteKey = $Site.URL + "/"Try {$OneDriveHashTable.Add($Site.Owner, $SiteKey)} Catch {# Write-Host ("Error {0} when adding {1} to hash table" -f $_.Exception.Message, $Site.Owner)}}Write-Host "Looking for users with OneDrive licenses..."# OneDrive doesn't have a service plan, but we can use SharePoint Online (5dbe027f-2339-4123-9542-606e4d348a72)[guid]$SPOPlanId = "5dbe027f-2339-4123-9542-606e4d348a72"[array]$Users = Get-MgUser -filter "assignedPlans/any(s:s/serviceplanid eq $SPOPlanId and capabilityStatus eq 'Enabled')" `-ConsistencyLevel eventual -CountVariable Test -All -PageSize 999 `-Property Id, displayName, userprincipalName, assignedLicenses, assignedPlans, department, country `-Sort displayNameWrite-Host ("Found {0} users with SharePoint (OneDrive) licenses" -f $Users.Count)# Check each user to see if a OneDrive site exists. If not, force its provisioning[int]$CountOfProvisionedSites = 0ForEach ($User in $Users) {If ($OneDriveHashTable.ContainsKey($User.UserPrincipalName)) {Write-Host ("User {0} already has a OneDrive for Business site" -f $User.UserPrincipalName)} Else {Write-Host ("User {0} does not have a OneDrive site" -f $User.UserPrincipalName)# Provision the OneDrive siteRequest-SPOPersonalSite -UserEmails $User.userprincipalName -NoWait$CountOfProvisionedSites++Write-Host ("Requested provisioning of OneDrive for Business site for {0}" -f $User.UserPrincipalName) -ForegroundColor Yellow}}Write-Host ("Provisioned {0} OneDrive sites" -f $CountOfProvisionedSites) -ForegroundColor Green
Attribution
Author
Office365itpros