Create folders in OneDrive For Business using PowerShell
A few years back, I needed to create folders in OneDrive for Business in preparation for a migration. And as PowerShell PnP (Patterns & Practices) didn’t exist, I used CSOM (Client Side Object Model). Now that we have PowerShell PnP, we can accomplish so much with only a few lines of code, and it’s more “admin-friendly“.
In this blog post, we are going to see the process of creating folders in OneDrive for Business, and also what I call “clean up after yourself”.
Process overview
There’s a process to follow if you want things to go smoothly.
So here we go:
- Pre-provision each OneDrive for Business in order to get the URL (the web address needs to exist if we want to access the OneDrive)
- Get each user’s OneDrive for Business URLs (to add second admin in step 3 below)
- Add a second Site Collection Admin on each OneDrive for Business (remember only the user is an admin otherwise)
- Create the folders on each OneDrive for Business
- Remove the second Site Collection Admin from each OneDrive for Business (“clean after yourself” for security reasons!)
Pre-provision each user’s OneDrive For Business
When you license a user for SharePoint Online, the OneDrive for Business URL is not automatically created. The user needs to access his/her Office 365 account, click on OneDrive, and a few “next, next, next“… But sometimes for a better user experience, you’d want to pre-provision OneDrive beforehand.
You can use a simple .csv
file in your script, or enter the usernames manually when using the New-PnPPersonalSite
cmdlet if only a limited number (like below).
|
|
Get each user’s OneDrive For Business URL
Now that the URL has been created, let’s export into a .csv file the URLs we are interested in, by using the same .csv file we used earlier. To retrieve the URLs, we’ll use the Get-PnPUserProfileProperty
cmdlet.
|
|
The results look like this:
Add a second Site Collection Admin on each OneDrive For Business
First, why do we need to add another Site Collection Admin? Well because by default, only the user is a Site Collection admin. Which is normal when you think about… It’s their own personal space. So even with a Global Admin account, you’ll get an Access Denied.
So in order to create the folders, then we need to add our account.
To that end, we’ll have to use the Set-SPOUser
cmdlet (from the SharePoint Online module by Microsoft) on each user’s OneDrive for Business site. The reason for not using the Add-PnPSiteCollectionAdmin
from the PnP module is that this command needs to be run in the current context. Meaning we need to be able to connect to that site first, and then add an account.
Therefore, it’s not possible for OneDrive for Business as that’s what we’re trying to achieve: Add our own account.
|
|
Create folders in OneDrive For Business
|
|
Remove the second Site Collection Admin from OneDrive For Business
Now that we have completed our task(s), let’s remove our account for security reasons.
Very simple process as we are using the exact same script as in step 3 when adding the account, with the only difference of -IsSiteCollectionAdmin $false
If you are still connected to SharePoint with the SPO module, you don’t need to do it again.
|
|
Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"
to get the URLs already available in your tenant.Thanks for reading!