Create folders in OneDrive For Business using PowerShell PnP – What do I need to do?

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 SharePoint 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:

  1. 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)
  2. Retrieve each user’s OneDrive for Business URLs (to add second admin in step 3 below)
  3. Add a second Site Collection Admin on each OneDrive for Business (remember only the user is an admin otherwise)
  4. Create the folders on each OneDrive for Business
  5. Remove the second Site Collection Admin from each OneDrive for Business (“clean after yourselffor security reasons!)

⚠️ This process assumes that the users are already licensed for SharePoint Online.

 

1) Pre-Provision each 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).

#Connect to SPO
Connect-PnPOnline -Url https://<TENANT_NAME>-admin.sharepoint.com

#Pre-provision the ODFB to create the URLs
$myUsers = Import-Csv -Path 'C:\users\$env:USERNAME\Desktop\UsersToPreProv.csv'
New-PnPPersonalSite -Email $myUsers

ℹ️ My csv file only contains one header called “UserPrincipalName” (without quotes) with all the users UPN

 

 

2) Retrieve 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.

#Import users
$myUsers = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\UsersToPreProv.csv"

#Retrieve the URLs + export them
$ODFBurls = @()
foreach ($user in $myUsers) {
    $ODFBurls += Get-PnPUserProfileProperty -Account $user.UserPrincipalName
}
$ODFBurls | Select-Object PersonalUrl | Export-Csv -Path "C:\users\$env:USERNAME\desktop\URLs.csv" -NoTypeInformation

The results should look like this:

ODFBUrls

 

 

3) Add a 2nd 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.

#Connect to SharePoint Admin Center using the SPO module
$creds = Get-Credential
Connect-SPOService -Url https://<TENANT_NAME>-admin.sharepoint.com -Credential $creds

#Import users
$userURLs = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\URLs.csv"

#Store 2nd Admin account into a variable
$adminAcctToAdd = "myAdmin@domain.com"

#Add 2nd Site Collection admin
foreach ($url in $userURLS) {
    Write-Host "Connecting to: $($url.PersonalUrl) and adding user $($adminAcctToAdd)" -ForegroundColor Green
    Set-SPOUser -Site $url.PersonalUrl -LoginName $adminAcctToAdd -IsSiteCollectionAdmin $true
}

ℹ️ If you have your credentials stored in the Credential Manager, you can remove the $creds = Get-Credential, and use -Credential (Get-PnPStoredCredential -Name <your_value> -Type PSCredential) 

 

 

4) Create folders in OneDrive for Business

In this step, you can also have a look at my other post to learn how to create folders, add files, etc…

#Import URLs
$userURLs = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\URLs.csv"

#Add a folder in each ODFB
foreach ($url in $userURLs) {
    Connect-PnPOnline -Url $url.PersonalUrl
    Add-PnPFolder -Name "Folder1" -Folder "Documents"
}

FolderCreated

 

 

5) Remove 2nd 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.

#Import users
$userURLs = Import-Csv -Path "C:\users\$env:USERNAME\Desktop\URLs.csv"

#Store 2nd Admin account into a variable
$adminAcctToRemove = "myAdmin@domain.com"

#Remove 2nd Site Collection admin
foreach ($url in $userURLs) {
    Write-Host "Connecting to: $($url.PersonalUrl) and adding user $($adminAcctToRemove)" -ForegroundColor Green
    Set-SPOUser -Site $url.PersonalUrl -LoginName $adminAcctToRemove -IsSiteCollectionAdmin $false
}

 

 

Good to know

ℹ️ If you run the following cmdlet, you will get the URLs already available within your tenant:
Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'"

 

ℹ️ In the new Office 365 portal, you have the possibility to create a link to access his/her OneDrive for Business if it has been pre-provisioned already.
Click on the user name >> OneDrive tab (when the blade opens) >> click on the link called Create link to files under “Get access to files”.
If the OneDrive is not pre-provisioned, you will see the following:

OneDriveNotPreprov

 

There you have it! Thanks for reading 😇

 

 

Leave a Reply

%d bloggers like this: