Change Specific Users’ OneDrive for Business Default Storage Quota using PowerShell PnP (With Error Handling for Different Outcomes!)

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.

 

The Csv File

Let’s create a simple CSV file with the (new) quotas & warning for each user.

IMPORTANT: Quotas need to be in BYTES.

โ„นย  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 and 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-PnPUserOneDriveQuota to change the quotas for each user

 

The Script

Now let’s put everything together ๐Ÿ™‚

#Connect to SPO
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

So 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

 

The user’s OneDrive For Business storage has changed (green) while the (organization) Default Storage hasn’t (yellow)

Users having problems are exported

 

There we go! Thanks for reading ๐Ÿ™‚

 

 

Leave a Reply

%d bloggers like this: