How to Create OneDrive for Business Site Collections using PowerShell?

Requirement: Pre-Create OneDrive for Business Sites using PowerShell.

OneDrive for Business is a Microsoft's cloud-based storage solution that lets users to store and work on files that are personal to them. Its similar to a SharePoint Online document library with full control granted to users, so that they can add, update, and delete files or folders in your OneDrive. 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 "[email protected]","[email protected]" -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 vs OneDrive for Business:

  • OneDrive is your personal online storage which you can get with Microsoft accounts (E.g. Outlook.com) with initial 15GB disk space. You can use your personal OneDrive to store documents, photos, videos, etc in the cloud and share them with your friends and family. You can use the OneDrive client to sync your files and easily access them anywhere from Windows, Mac and mobile devices.
  • OneDrive for Business: OneDrive for business sites are intended for business and offered through your work account by Office 365 to store, share, and sync all work files. You get 1TB of storage for individual use typically. Your account settings such as external sharing, storage, access are managed by your organization. You can install the OneDrive for Business client to sync your files to your devices. 


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="[email protected]","[email protected]"

#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 "[email protected]"

#Pre-Provision OneDrive for Business Site for Multiple users
New-PnPPersonalSite -Email "[email protected]", "[email protected]"
Please note, it take a moment for this cmdlet to provision OneDrive for business sites.

No comments:

Please Login and comment to get your questions answered!

Powered by Blogger.