Contents

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:

  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. Get 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 yourself” for security reasons!)
Licenses
This process assumes users already have a SharePoint Online license assigned.

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

1
2
3
4
5
6
#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

 

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.

1
2
3
4
5
6
7
8
9
#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 look like this:

/images/powershell-screenshots/create-folders-in-odfb-img1.png
 

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
#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
}

 

Create folders in OneDrive For Business

1
2
3
4
5
6
7
8
#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"
}

 

/images/powershell-screenshots/create-folders-in-odfb-img2.png
 

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.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
#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
Run: Get-PnPTenantSite -IncludeOneDriveSites -Filter "Url -like '-my.sharepoint.com/personal/'" to get the URLs already available in your tenant.

Create a link via the UI
In the new Office 365 portal, you have the possibility to create a link to access the user’s 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”.

 

Thanks for reading!