How to Create OneDrive for Business Site Collections using PowerShell?

Requirement: OneDrive sites in Office 356 are typically created when they click on the OneDrive link for the first time. In my case, for a file share to SharePoint Online OneDrive migration project, I had to pre-create OneDrive for Business sites for N number of users.

How to Create OneDrive Sites using PowerShell?
OneDrive for Business sites can be pre-created using a PowerShell cmdlet Request-SPOPersonalSite. Open SharePoint Online Management Shell, Connect as SharePoint Online Administrator by entering the appropriate credentials to the prompt and run this script to provision OneDrive for business sites using PowerShell.
$AdminSiteURL="https://crescent-admin.sharepoint.com"

#Get Credentials to connect to SharePoint Admin Center
$Cred = Get-Credential
 
#Connect to SharePoint Online Admin Center
Connect-SPOService -Url $AdminSiteURL –credential $Cred

#sharepoint online create mysite powershell
Request-SPOPersonalSite -UserEmails "peter@crescent.com","john@crescent.com" -NoWait $True

Wait for new Minutes, SharePoint Online timer job provisions My Sites for users given in the script. You can specify up to 200 Max. Please note, This cmdlet may fail if the account is disabled or password expired.  You can verify the created OneDrive sites using:
Get-SPOSite -Template "SPSPERS" -Limit ALL -includepersonalsite $True
onedrive for business create personal site

This lists all OneDrive site collections in your tenant.

OneDrive for Business: Create Personal Site
Let's create new OneDrive sites for existing users using PowerShell CSOM.
#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 parameter values
$AdminSiteURL="https://crescent-Admin.sharepoint.com/"
$UserAccounts="peter@crescent.com","john@crescent.com"

#Get Credentials to connect
$Cred= Get-Credential
$Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.Username, $Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($AdminSiteURL)
    $Ctx.Credentials = $Credentials

    #Get Profile Loader Object
    $ProfileLoader=[Microsoft.SharePoint.Client.UserProfiles.ProfileLoader]::GetProfileLoader($Ctx)

    #Create onedrive for business sites
    $ProfileLoader.CreatePersonalSiteEnqueueBulk($UserAccounts)
    $ProfileLoader.Context.ExecuteQuery()

    Write-host -f Green "OneDrive for Business Sites Creation Successfully Queued!"
}
Catch {
        write-host -f Red "Error Creating OneDrive for Business Sites!" $_.Exception.Message
}
Now we started the migration project from file shares to OneDrive so that users get many new features such as : version history, metadata, recycle bin, permissions, check in/check out, auditing, web interface, etc.

Pre-Create OneDrive for Business Sites using PnP PowerShell
OneDrive for business sites gets created when the user tries to login to the site for the first time. There are situations where user never logged in and therefore no OneDrive sites created! We had to migrate the data to the users personal site from network file share.

So, How to create personal site or OneDrive for business site for the user? Well, with this PnP PowerShell script, you can simply create the OneDrive for Business sites! Either one user at a time or multiple users!

#Config Variables
$TenantSiteURL =  "https://crescent-admin.sharepoint.com/"

#Connect to PnP Online
#Connect-PnPOnline -Url $TenantSiteURL -UseWebLogin

#Create OneDrive for User
New-PnPPersonalSite -Email "sfalam@crescent.com"

#Pre-Provision OneDrive for Business Site for Multiple users
New-PnPPersonalSite -Email "latam@crescent.com", "saopaulo@crescent.com"
Please note, it take a moment for this cmdlet to provision OneDrive for business sites.
How to Create OneDrive for Business Site Collections using PowerShell? How to Create OneDrive for Business Site Collections using PowerShell? Reviewed by Salaudeen Rajack on October 21, 2017 Rating: 5

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.