top of page

Pre-Provisioning User OneDrives with PowerShell Made Easy

  • Writer: Jack Davies
    Jack Davies
  • Jun 22
  • 5 min read

Updated: Jul 5

Before we touch the scripts, we need to ensure your environment is ready. This involves having the right permissions and the necessary PowerShell modules installed.

Close-up of computer screen displaying colorful code lines in a dark setting, with a blurred keyboard in the foreground.

Prerequisites:

Step 1: Verify Your Permissions You must have one of the following administrative roles in your Microsoft 365 tenant:

  • Global Administrator

  • SharePoint Administrator


Step 2: Install the Required PowerShell Modules These scripts rely on two key modules to interact with your tenant. If you've never run these commands before, or if it's been a while, it's best to run them to ensure you have the latest versions.

Open PowerShell as an Administrator and run the following commands:


Install-Module -Name Microsoft.Online.SharePoint.PowerShell -Force
Install-Module -Name Microsoft.Graph -Force

Scenario 1 - Provisioning OneDrive for ALL users


This is the script you'll use for initial tenant setups, preparing for a company-wide migration, or ensuring every existing user has a OneDrive site ready to go.

# --Created by Jack @ jackdjd.com--

# 1. CONNECT TO MICROSOFT 365 SERVICES

# Connect to SharePoint Online
try {
    $sharePointAdminUrl = Read-Host -Prompt "Enter your SharePoint Admin Center URL (e.g., https://yourtenant-admin.sharepoint.com)"
    Connect-SPOService -Url $sharePointAdminUrl
}
catch {
    Write-Host "Error connecting to SharePoint. Please check the URL and your permissions." -ForegroundColor Red
    return
}

# Connect to Microsoft Graph
try {
    Connect-MgGraph -Scopes "User.Read.All"
}
catch {
    Write-Host "Error connecting to Microsoft Graph." -ForegroundColor Red
    return
}


# 2. GET AND VALIDATE USERS (CLIENT-SIDE FILTERING METHOD)

Write-Host "Retrieving all users to filter locally. This is the most reliable method." -ForegroundColor Green
try {
    # Step 1: Get ALL users and the properties we need to check.
    Write-Host "Step 1: Fetching all user objects from Microsoft 365..."
    $allUsers = Get-MgUser -All -Property "UserPrincipalName,DisplayName,UserType,UsageLocation"

    # Step 2: Filter the results locally using PowerShell's Where-Object.
    # This avoids server-side filter errors entirely.
    Write-Host "Step 2: Filtering for 'Member' accounts with a Usage Location..."
    $usersToProcess = $allUsers | Where-Object { $_.UserType -eq 'Member' -and (-not [string]::IsNullOrWhiteSpace($_.UsageLocation)) } | Select-Object UserPrincipalName, DisplayName
    # --- END OF CORRECTION ---

    if ($null -eq $usersToProcess -or $usersToProcess.Count -eq 0) {
        Write-Host "No users found matching the criteria after filtering. Exiting." -ForegroundColor Yellow
        return
    }

    Write-Host "Successfully found $($usersToProcess.Count) users to process." -ForegroundColor Green
    
    # Diagnostic Step: Display the first few users found to ensure they are valid.
    Write-Host "--- Checking the first 5 users found ---" -ForegroundColor Cyan
    $usersToProcess | Select-Object -First 5 | Format-Table
    Write-Host "------------------------------------" -ForegroundColor Cyan

}
catch {
    # This catch block will now only trigger for major issues, like being unable to connect.
    Write-Host "A critical error occurred while retrieving users: $($_.Exception.Message)" -ForegroundColor Red
    return
}

# 3. PROVISION ONEDRIVE FOR EACH USER

Write-Host "Starting the OneDrive provisioning process..." -ForegroundColor Cyan

foreach ($user in $usersToProcess) {
    # Sanity Check: Verify the user object is not null before proceeding.
    if ($null -eq $user -or [string]::IsNullOrWhiteSpace($user.UserPrincipalName)) {
        Write-Warning "Skipping an invalid or empty user object found in the collection."
        continue # Skips to the next user in the loop
    }

    $userPrincipalName = $user.UserPrincipalName
    $displayName = $user.DisplayName

    try {
        Write-Host "Sending provisioning request for $displayName ($userPrincipalName)..."
        Request-SPOPersonalSite -UserEmails $userPrincipalName -NoWait
        Write-Host "  -> Success: Request has been queued for $userPrincipalName." -ForegroundColor Green
    }
    catch {
        Write-Host "  -> Info: An issue occurred for $userPrincipalName. They may already have a OneDrive, lack a license, or another issue occurred." -ForegroundColor Yellow
    }
}

# 4. DISCONNECT

Write-Host "OneDrive provisioning script has completed." -ForegroundColor Green

Step-by-Step Guide to Running the Script

  1. Save the Code: Copy the script above into a text editor (like VS Code or Notepad) and save it as

  2. Open PowerShell: Launch PowerShell as an Administrator.

  3. Navigate to the File: Use the cd command to navigate to the directory where you saved your script. For example: cd C:\Scripts

  4. Run the Script: Execute the script by typing its name: .\Provision-AllOneDrives.ps1.

  5. Follow the Prompts:

    • First, you will be prompted to enter your SharePoint Admin Center URL.

    • Next, browser windows will pop up asking you to authenticate to SharePoint and Microsoft Graph. Sign in with your administrator account.

  6. Monitor the Output: The script will provide real-time feedback, telling you it's connecting, how many users it found, and a sample of those users. It will then loop through each user and print a status message.


Scenario 2 - Provisioning OneDrive for a single user


This script is your go-to for targeted tasks: onboarding a single new employee, fixing a provisioning issue for a specific user, or simply testing the process. It takes the user's email address as a direct parameter.


# --Created by Jack @ jackdjd.com--

param (
    [Parameter(Mandatory=$true, HelpMessage="Enter the User Principal Name (email address) of the user.")]
    [string]$UserPrincipalName
)

# Section 1: Connect to Microsoft 365 Services
Write-Host "Connecting to Microsoft 365 services..." -ForegroundColor Cyan
try {
    $sharePointAdminUrl = Read-Host -Prompt "Enter your SharePoint Admin Center URL (e.g., https://yourtenant-admin.sharepoint.com)"
    Connect-SPOService -Url $sharePointAdminUrl
    Connect-MgGraph -Scopes "User.Read.All"
}
catch {
    Write-Host "A critical error occurred during connection: $($_.Exception.Message)" -ForegroundColor Red
    return
}

# Section 2: Validate the User and Provision
Write-Host "Validating user '$UserPrincipalName'..." -ForegroundColor Green
try {
    $user = Get-MgUser -UserId $UserPrincipalName
    if ($null -eq $user) {
        Write-Host "Error: User '$UserPrincipalName' not found." -ForegroundColor Red
        return
    }

    Write-Host "Sending provisioning request for $($user.DisplayName) ($($user.UserPrincipalName))..." -ForegroundColor Cyan
    Request-SPOPersonalSite -UserEmails $user.UserPrincipalName -NoWait
    Write-Host "  -> Success: Request has been queued." -ForegroundColor Green
}
catch {
    Write-Host "  -> An error occurred for $UserPrincipalName. They may already have a OneDrive, lack a license, or the UPN is incorrect." -ForegroundColor Red
    Write-Warning $_.Exception.Message
}


# Section 3: Disconnect
Write-Host "Script has completed. Disconnecting from services." -ForegroundColor Green

Step-by-Step Guide to Running the Script

  1. Save the Code: Copy the script above and save it as Provision-SingleOneDrive.ps1

  2. Open PowerShell: Launch PowerShell as an Administrator.

  3. Navigate to the File: cd C:\Scripts

  4. Run the Script with a Parameter: This is the key difference. You must provide the user's email address (User Principal Name) using the -UserPrincipalName parameter.

.\Provision-SingleOneDrive.ps1 -UserPrincipalName "john.doe@example.com"
  1. Follow the Prompts: Just like before, you will be prompted for the SharePoint Admin URL and to sign in. The script will then target only the user you specified.


Automating OneDrive provisioning is a fundamental skill for any modern workplace administrator. By leveraging the power of PowerShell, you move from a reactive to a proactive stance, ensuring your users have the tools they need right when they need them. These scripts provide a reliable, robust, and efficient foundation for managing your Microsoft 365 environment at scale. Keep them in your toolkit—they're bound to come in handy.

how to provision onedrive for all users powershell , troubleshoot onedrive provisioning errors , sharepoint online management shell guide , provision onedrive for single user powershell , force onedrive creation for all users

bottom of page