Contents

Change specific users OneDrive For Business quotas using PowerShell

 

It may be useful to allocate specific users with specific OneDrive for Business storage quotas. But we may be faced with some challenges trying to achieve that… In this blog post, we’ll use a .csv file containing the users we wish to change the quota for, as well as adding some error handling to our script to manage the outcomes 🧐

And to top it off, we’ll export the “users not found” so you’re not in the dark when closing your PowerShell console!

What could the errors be?

We’re interacting with 2 different things here:

  • Usernames (i.e.: User Principal Names)
  • OneDrive for Business (not auto-magically provisioned!)

So thinking about our script, maybe we have a user who’s not in our tenant (typo?) or maybe his/her OneDrive is not provisioned? If OneDrive is not provisioned, it means either the user doesn’t have a license for SharePoint Online, or he/her simply didn’t access it at all.

Script logic

We’ll have 2 parts in this script:

  1. Change the quota for specific users
  2. Handles errors differently depending on the type (more details below) Let’s get started.

Csv file

Let’s create a simple CSV file with the (new) quotas & warning for each user. Our quotas need to be in BYTES.

/images/powershell-screenshots/change-onedrive-quotas-img1.png

Excel cells format
Looking at the format for the cells, we have this “1.07E+11” thingy… Make sure your cell is TEXT, and when you click on it, it should show the full numbers in the Excel formula bar.

Cmdlets & Variables

We’ll use 3 variables and 1 cmdlet here.

  • $usernames to import our CSV file containing the users
  • $results to store the users having problems
  • $logFile to export the users having problems
  • Set-PnPUserOneDriveQuotato change the quotas for each user

Script

Now let’s put everything together 🙂

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Connect-PnPOnline -Url "https://<TENANT-NAME>-admin.sharepoint.com"

#Import CSV file with usernames
$usernames = Import-Csv -Path "<YOUR-CSV-FILEPATH>"

#Results for storing the users not found (or not provisioned)
$results = @()
$logFile = "C:\users\$env:USERNAME\Desktop\usersNotFound.csv"

#Script
foreach($user in $usernames){
    try{
        Set-PnPUserOneDriveQuota -Account $user.username -Quota $user.newQuota -QuotaWarning $user.newWarning -ErrorAction Stop | Out-Null
    }
    
    catch [Microsoft.SharePoint.Client.ServerException] {
        Write-Host "User not found: $($user.username)" -ForegroundColor Cyan
        Write-Warning $error[0]
        
        if ($error[0].Exception -like "*Unknown Error*"){
            $results += [pscustomobject]@{
                userNotFound        = $user.username
                ODFBProvisioned  = "No"
                userInTenant     = "Yes"
            }
        }
        else {
            $results += [pscustomobject]@{
                userNotFound    = $user.username
                ODFBProvisioned = "N/A"
                userInTenant    = "No"
            }
        }
    }
}
$results | Export-Csv -Path $logFile -NoTypeInformation

What are we doing here?

  1. We use a Try/Catch to handle the errors
  2. We loop through each user in our CSV file and (try to) set the new values
  3. We catch 2 possible errors:
    • “Unknown error” when OneDrive is not provisioned
    • “Something else” when the user doesn’t exist in our tenant
  4. We export the results to a file

Results

Here is what it would look like 👇

Running the script:

  • BiancaP doesn’t have a ODFB provisioned (but she’s part of our the tenant) –-> Unknown error
  • user123 is not part of our tenant (or maybe you have a typo!) –-> User not found in the domain

/images/powershell-screenshots/change-onedrive-quotas-img2.png

 

Org default storage
The user’s OneDrive For Business storage has changed (green) while the (organization) default storage hasn’t (yellow).

/images/powershell-screenshots/change-onedrive-quotas-img3.png  

Users with problems are also exported:

/images/powershell-screenshots/change-onedrive-quotas-img4.png

There we go, thanks for reading!