Provisioning Onedrive for Business for all your users

Since the inception of the OneDriveMapper script, I’ve often been asked if there is a way to pre-provision Onedrive for Business storage for users.

When a user signs into Office 365 for the first time and clicks Onedrive, their Onedrive for Business storage will be allocated and initialized, before that, it is not possible to map their OneDrive storage, or sometimes more importantly: to migrate data to it.

As a good consultant or IT admin, you don’t want to force your users to do this before they can map their drive or before you migrate their data because you like to automate things, repetitive or manual process tend to be unreliable.

Doing this the smart way, is scripting it. Of course your users already have to have a license.

Don’t forget to set administrative permissions on all accounts afterwards, if you intend to migrate data there for your users.

The following script will pre-provision OneDrive for ALL users in your Office 365 environment.

ODFB_BP_v0.2

Source:


########
#ODFB Bulk Provisioning
#Copyright: Free to use, please leave this header intact
#Author: Jos Lieben (OGD)
#Company: OGD (http://www.ogd.nl)
#Script help: http://www.lieben.nu
#Purpose: Provision Onedrive for all users in Office 365
########
#Changes:
#V0.2 fixed a bug, .Clear() does not function as expected in Powershell, = @() does.
#V0.21 changed the default batch size to 10, this seems to work better (reported by Iain)
########
#Requirements:
########
<#MS Online Services Signin Assistant:
https://www.microsoft.com/en-us/download/details.aspx?id=41950
Azure AD Module (x64):
http://social.technet.microsoft.com/wiki/contents/articles/28552.microsoft-azure-active-directory-powershell-module-version-release-history.aspx
Powershell 4
.NET 4.5
Sharepoint Server 2013 Client Components
https://www.microsoft.com/en-us/download/details.aspx?id=42038
run “Set-Executionpolicy Unrestricted” in an elevated powershell window
Windows 7+ or Windows Server 2008+
#>

$o365login = "admin@ogdemo1.onmicrosoft.com" #Username of O365 Admin
$o365pw = "YourPassword" #Password of O365 Admin
$logfile = ($env:APPDATA + "\ODFB_BP.log") #Logfile in case of errors
$spURL = "https://ogdemo1-admin.sharepoint.com" #URL to your SP Admin site $batch_size = 10 #Maximum accounts to provision at once, technet lists 200 as the max, but users have reported 10 is optimal 

#Start script
ac $logfile "-----$(Get-Date) ODFB_BP v0.2 $($env:COMPUTERNAME) Session log-----`n"

#build Credential Object
$secpasswd = ConvertTo-SecureString $o365pw -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential ($o365login, $secpasswd)

#Load sharepoint module
try{
 [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime") | Out-Null [System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.UserProfiles") | Out-Null
}catch{
 $errorstring = "ERROR: Failed to load Sharepoint Libraries, exiting"
 ac $logfile $errorstring
 Write-Host $errorstring
 Pause
 Exit
}
#load Azure module
$env:PSModulePath += ";C:\Windows\System32\WindowsPowerShell\v1.0\Modules\"
try{
 Import-Module msonline
}catch{
 $errorstring = "ERROR: Failed to load Azure module, exiting"
 ac $logfile $errorstring
 ac $logfile $error[0]
 Write-Host $errorstring
 Pause
 Exit
}
#connect to MSOL
try{
 Connect-MsolService -Credential $Credentials
}catch{
 $errorstring = "Critical error, unable to connect to O365, check the credentials"
 ac $logfile $errorstring
 ac $logfile $error[0]
 Write-Host $errorstring
 Pause
 Exit
}

#fetch all UPN's
$users = Get-MsolUser -All | Select-Object UserPrincipalName

#Build sP object
$client = New-Object Microsoft.SharePoint.Client.ClientContext($spURL) $clientweb = $client.Web $client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($o365login,$secpasswd) #Connect $client.Load($clientweb) try{ $client.ExecuteQuery() }catch{ $errorstring = "Critical error, unable to connect to Sharepoint Online, check admin url and credentials"
 ac $logfile $errorstring
 ac $logfile $error[0]
 Write-Host $errorstring $error[0]
 Pause
 Exit } $loader =[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($client) $profile = $loader.GetUserProfile() $client.Load($profile) $client.ExecuteQuery() #enqueue per batch_size users (max is 200 users per batch) $total = $users.Count $batchjob = @() for($i = 0; $i -lt $total; $i++){ $batchjob += $users[$i].UserPrincipalName if($i+1 -eq $total -or $batchjob.Count -gt $batch_size){ #enqueue in loader try{ $loader.CreatePersonalSiteEnqueueBulk($batchjob) $loader.Context.ExecuteQuery() }catch{ $errorstring = "Critical error, unable to create bulk provisioning job in Sharepoint Online"
 ac $logfile $errorstring
 ac $logfile $error[0]
 ac $logfile $batchjob
 Write-Host $errorstring $error[0] $batchjob } $batchjob = @() } } ac $logfile "Script finished" Write-Host "Job Finished" Pause Exit 

 

Script was inspired by Frank Marasco’s post on technet.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

46 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Nick
Nick
8 years ago

I keep getting the following errors. The username and password and site fields have been updated correctly in the ps1 New-Object : Cannot find type [Microsoft.SharePoint.Client.ClientContext]: verify that the assembly containing this type is loaded. At \\engineer\store$\Scripts\Powershell\OneDrive-ProvisionAllUsers.ps1:81 char:11 + $client = New-Object Microsoft.SharePoint.Client.ClientContext($spURL) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand New-Object : Cannot find type [Microsoft.SharePoint.Client.SharePointOnlineCredentials]: verify that the assembly containing this type is loaded. At \\engineer\store$\Scripts\Powershell\OneDrive-ProvisionAllUsers.ps1:83 char:23 + $client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCre … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidType: (:) [New-Object], PSArgumentException + FullyQualifiedErrorId : TypeNotFound,Microsoft.PowerShell.Commands.NewObjectCommand You cannot call a method on a… Read more »

David
David
5 years ago

There is now a SPO Module command that pre-provisions OneDrive sites: https://docs.microsoft.com/en-us/powershell/module/sharepoint-online/Request-SPOPersonalSite

Andy Harper
6 years ago

Hi Jos, our school has been a big supporter of yours for years. Need some assistance please. OneDrive Provision tool and also add onedrive permissions tool are broken. Been using for a couple years and last time used was about 2 weeks ago. Error for ODFB_BP occurs here:

Also when ODFB_RA runs it will gather the list and when it’s ready to assign permissions, it will fail on the same portion.

Connect-MsolService -Credential $Credentials
}catch{
$errorstring = “Critical error, unable to connect to O365, check the credentials”
ac $logfile $errorstring
ac $logfile $error[0]
Write-Host $errorstring
Pause
Exit

Kyle
Kyle
7 years ago

Hello, I keep running into the following errors and I’m not sure what to do. All I’ve done to the script is fill in my Global Admin credentials and the site URL for my Sharepoint admin site Unable to find type [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]. At C:UsersSetupDesktopODFB_BP_v0.2.ps1:98 char:10 + $loader =[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::Ge … + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (Microsoft.Share…s.ProfileLoader:TypeName) [], RuntimeException + FullyQualifiedErrorId : TypeNotFound You cannot call a method on a null-valued expression. At C:UsersSetupDesktopODFB_BP_v0.2.ps1:99 char:1 + $profile = $loader.GetUserProfile() + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (:) [], RuntimeException + FullyQualifiedErrorId : InvokeMethodOnNull Cannot find an overload for “Load” and the argument… Read more »

Alpa
Alpa
8 years ago

How can I get to all users on my-site onedrive using powershell script.? I ran a script to update secondary owner. I can’t find a way to check using powershell script.

Alpa
Alpa
8 years ago

Hi Jos
I downloaded ODFB file and modified code according to our onedrive site.
I am running into error on below line of script
$NumProfiles = $UserProfileService.GetUserProfileCount(-1)
it cannot be null.
How do I fix this.
Also,
I want to provision onedrive user profile and remove site administrator access one at a time.
How do I do this..
Your suggestion would be very helpful

S Fitton
S Fitton
8 years ago

I’m struggling to get this to work. I receive the following error: Connect-MsolService : The user name or password is incorrect. Verify your user name, and then type your password again. At D:\ODFB_BP_v0.2.ps1:65 char:5 + Connect-MsolService -Credential $Credentials + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [Connect-MsolService], MicrosoftOnlineException + FullyQualifiedErrorId : 0x80048821,Microsoft.Online.Administration.Automation.ConnectMsolService Get-MsolUser : You must call the Connect-MsolService cmdlet before calling any other cmdlets. At D:\ODFB_BP_v0.2.ps1:76 char:10 + $users = Get-MsolUser -All | Select-Object UserPrincipalName + ~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [Get-MsolUser], MicrosoftOnlineException + FullyQualifiedErrorId : Microsoft.Online.Administration.Automation.MicrosoftOnlineException,Microsoft.Online.Adm inistration.Automation.GetUser I’m certain the username & password are correct I have even… Read more »

Rico
Rico
8 years ago

Your script is exactly what I was looking for. I did make some modifications. I did not like that I had to store my credentials in the code itself so I changed it to prompt for credentials. I hope you and or someone else can use them

I replaced

Lines 26 -27 with
write-host “Please enter your Email”
$o365login = read-host #Username of O365 Admin
write-host “Please enter your password”
$o365pw = read-host -AsSecureString #Password of O365 Admin

Lines 26 -27 with
$Credentials = New-Object System.Management.Automation.PSCredential ($o365login, $o365pw)
and line 93 with
$client.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials ($o365login,$o365pw)

David
David
8 years ago

Hi Jos
After the script runs, I can see under the Sharepoint Admin center that the number of user profiles is correct.

Under manage user profiles I can see all users, however if I attempt to manage a users site in any way I receive the error “The user has not created a personal site”

Any ideas?

Nick
Nick
8 years ago

I installed the Sharepoint remote management tools and ran the script again and got the following error on about 60 users. Does this mean it completed correctly on the other users?

Critical error, unable to create bulk provisioning job in Sharepoint Online Method invocation failed because [Microsoft.SharePoint.Client.UserProfiles.ProfileLoader] does not contain a method named ‘CreatePersonalSiteEnqueueBulk’

Mitch McEvoy
Mitch McEvoy
8 years ago

Hi Jos,
Thanks for this article is there a way to limit this to only one domain if our tenant has multiple?

Thanks,
Mitch

Grateful Reader
Grateful Reader
8 years ago

Thank you for this useful article and script. I’m adding this comment to help others who might run into the same issue I had.

“Critical error, unable to create bulk provisioning job in Sharepoint Online”
“You cannot call a method on a null-valued expression.”

My problem was this: For this script to run, the admin account running it must have been given a OneDrive license, and had the OneDrive profile provisioned.

While this makes perfect sense (reading the script), it wasn’t obvious to me at the start. I was testing using an admin account with no license assigned.

Michel
Michel
8 years ago

Hello Jos,
I have run ODFB_BP on one tenant and it work without an hitch creating users my-sharepoint successfully. It runs for about 15-20 minutes
Now I’m runnning it on another tenant, it runs without throwing any error for about 2 minutes and does not create a single space.
Ideas?
thank you

Carl Olsen
Carl Olsen
8 years ago

Just to add some details, it took me a while to get this script running. Make sure you have
msoidcli_64.msi installed (Microsoft Online Sign in)
You also need:
Sharepoint Online Management Shell from Microsoft
also very important
Sharepoint Client Components Online (Dont get the 2013 SDK this is version 15 and wont work). you need the Online SDK version 16

Raleigh Merkley
Raleigh Merkley
8 years ago

By default, the first time that a user browses to their newsfeed, site, or a OneDrive link, a OneDrive for Business site is automatically provisioned for them. Your organization has a custom process for adding new employees, and you want to create OneDrive for Business sites when you add new employees.

Ash K
Ash K
8 years ago

Hi Jos,

Another great script, keep it going 🙂 . Jos i was wondering if its possible with the script to provision selected users? i.e. users whom we provide the UPNs of in a CSV file say rather than the whole tenant?

Just wondering if the script could be update to process a csv file as a parameter for extra flexibility, this way we have best of both worlds i.e. for people who want to do the whole lot or subset of users.

Thanks,

Ash.

Iain
Iain
8 years ago

Hi Jos,

I’m attempting to use your script to provision onedrive for all users in a school I work in (approx 2800). It succesfully does the first 99, but then gives the “Critical error, unable to create bulk provisioning job in Sharepoint Online”. It’d be great if I could get it to continue on with the rest! I tried the other microsoft script where you enter a list of users and it only appears to provision the first 10. Any help would be greatly appreciated!

Thanks
Iain

Wolfgang
Wolfgang
8 years ago

Hi Jos,

We have recently come across your solution for the OneDriveMapper. Unfortunately, we have encountered some script errors and hanging-up errors. Is there any chance you could help our IT Deputy Manager sort this out?

Thanks a million,

Wolfgang