Office 365 Mass Provision PersonalSite/OneDrive

I needed to mass provision my preexisting accounts with OneDrive.  I searched online for viable scripts and nothing worked. All of the existing scripts used CreatePersonalSiteEnqueueBulk which is deprecated and no longer works. Request-SPOPersonalSite is the command that currently works. You have to be logged into SharePoint Online in powershell to use this command. I created a script that would provision any newly created users in a previous post.  However,  I still needed to provision all of my preexisting users, but I only need to run this script once.

This script takes a while.  Run this script at night, once you verify that it connects and works on a few users.  You can stop it and start it over again. This script will only provision users that need it.  This is not an automated script it requests a username when run.

The reason this script runs slow is that it requests the personal site for each user and if it receives an error then that user needs to be provisioned.

I needed to provision OneDrive so that users could log into iPad Office Applications. Also if a student did not have their OneDrive provisioned then, assignments could not be copied to their OneDrive via TeacherDashboard365.

Make sure to the items below are installed and up to date:

Prerequisites:

 

#Provision OneDrive/PersonalSite for Users that need it provisioned
#This runs slowly so run this overnight

#Install-Module -Name AzureAD 
Import-Module AzureAD
Import-Module Microsoft.Online.SharePoint.PowerShell -DisableNameChecking

#update and store password as necessary
$logfile2 = 'c:\temp\OneDrivesProvisioned.txt';



#Must be SharePoint Administrator URL
$webUrl = "https://[ORGANIZATION SITE]-admin.sharepoint.com";

#MySite web URL
$mysitewebUrl = "https://[ORGANIZATION SITE]-my.sharepoint.com";

$username = "GlobalAdmin@contoso.com";

#Credential for Office 365 
$mycreds = Get-Credential -Message "Office 365 Credential" -UserName $username




#Connect to msolservice to query users
Connect-AzureAD -credential $mycreds;

$filter = 'accountEnabled eq true';
$licenseduserList = Get-AzureADUser -All $True -Filter $filter | Where { ($_.AssignedLicenses -ne $Null -and $_.AssignedLicenses.Count -gt 0) -and !($_.UserPrincipalName -like "8*" -or $_.UserPrincipalName -like "9*")} | Sort-Object UserPrincipalName

Disconnect-AzureAD
Write-Host "OneDrive Provisioning";



#Connect to SharePoint Online to Provision OneDrive
Connect-SPOService -Url $webUrl -Credential $mycreds

Write-Host "Connected to site and Provision Search Started: $webUrl" -ForegroundColor Green;
    
#Test view all sites
#Get-SPOSite

    
if ($licenseduserList)
{
        
    $usercount = 1;
    $usersToProvision = @();
    $usersprovisioned = @();
    foreach ($eachuser in $licenseduserList)
    {

        #Run OneDrive/PersonalSite Provisioning in batches of 10 
        if ($usercount -gt 10)
        {
            Write-Host "Batch Provisioning OneDrive for users:";
            Write-Host "Users Needing Provision:";
            $usersToProvision;
            Request-SPOPersonalSite -UserEmails $usersToProvision;
            $usersToProvision = @();
            $usercount = 1;
        }
            
        #To enqueue
            
        $onedriveuser = [string]$($eachuser.UserPrincipalName);
        Write-Host $usercount
        #$usersToProvision += $onedriveuser;
        $spouser1 = $null;
        try
        {
            #ignore the current user if no error
            $spouser1 = Get-SPOUser -Site $mysitewebUrl -LoginName $onedriveuser
            $usersprovisioned += $onedriveuser;
        }
        catch
        {
            #users not provisioned with onedrive get caught
            $usersToProvision += $onedriveuser;
            Add-Content $logfile2 $onedriveuser;
            $usercount++;
            #Write-Host $onedriveuser
        }
            
           
         
    }

    #Run last batch
    if ($usercount -gt 1)
    {
        Write-Host "Batch Provisioning OneDrive for users:";
        Write-Host "Users Needing Provision:";
        $usersToProvision;
        Request-SPOPersonalSite -UserEmails $usersToProvision;
        $usersToProvision = @();
        $usercount = 1;
    }
}
else 
{
    Write-Host "No Users to Provision";
}

Disconnect-SPOService;

Write-Host "One Drive Provisioning Completed" ;


#to Test Provisioning succeeded wait at least a few hours
#Login to Office 365 using an account then hit the url below replacing username as desired
#replace @ symbol and . with underscores in the UserPrincipalName
#https://[ORGANIZATION SITE]-my.sharepoint.com/personal/USERPRINCIPALNAME

13 thoughts on “Office 365 Mass Provision PersonalSite/OneDrive

  1. Rakesh

    do you have any technet reference where it is mentioned that the method CreatePersonalSisafteEnqueueBulk has been deprecated…?

  2. Rakesh

    Thanks ! we have two instances of 0365 … on one environment it works fine but it does not work on the other but does not give any exceptions… I have tried Request-SPOPersonalSite and waited for 24 hours but still no sites created…

    1. Preston Post author

      I ran the Request-SPOPersonalSite with 10 or less accounts in the loop. Did you do the same? Also the problem could be related to permissions.

      If you run the script again 48 hours later and there are accounts that it is looping through then you know it did not work as expected.

  3. Scott Schradle

    I used CreatePersonalSiteEnqueueBulk yesterday and it worked for about 430 of 450 users. I passed an array of 200 at a time. I ran it yesterday afternoon and they were available this morning. The rest are blocked for login & I’m hoping that’s the issue. I changed my script to use request-SPOPersonalSite. I REALLY wish MS would build some feedback into that. You don’t even have to pass a valid username format..it just takes it an keeps going.

    Does anyone know a way to check if a site exists via script? I tried Test-SPOSite but that seems to fail on OneDrive sites. I need a way to know when the sites I create are actually created..

    1. Preston Post author

      The script in the article above uses Get-SPOUser in a try catch block to determine if the personal site is created or not. This script will only provision users that need to be provisioned.

  4. Anthony Balogun

    Thanks Preston, for the scripts provided. Where do you specify the users to be customised for personal site for onedrive in this script? Apologies in advance as I’m a newby in powershell

    1. Preston Post author

      Just under “#Get Licensed Users” you can filter more if desired. It is set to go through all users synched users, but will only provision users that need to be provisioned and it can be run more than once.

  5. umesh pandey

    $spouser1 = Get-SPOUser -Site $mysitewebUrl -LoginName $onedriveuser
    This commmand does not necessarily works as per expectation.

    1. Preston Post author

      That code will error if the OneDrive is not provisioned that is why it is in a try catch block. The catch adds the user to the queue for provisioning.

    1. Preston Post author

      I modified the article to have the full list of the prerequisite installs. I mentioned in the article previously to see my previous post regarding the prerequisites in bold and a link. It is easy to miss things sometimes.

Leave a Reply

%d bloggers like this: