Microsoft OneDrive plays a crucial role in cloud storage for users in an organization. When new employees join or when specific OneDrive sites are required, automating the OneDrive site creation process using PnP PowerShell can save time and improve efficiency.
This guide will walk you through the process of automating OneDrive site creation step by step, from installing PnP PowerShell to scripting and scheduling automation.
Step 1: Install and Update PnP PowerShell
To use PnP PowerShell, ensure it is installed on your system. Open PowerShell as an Administrator and run:
Install-Module -Name PnP.PowerShell -Force -AllowClobber
To update an existing installation:
Update-Module -Name PnP.PowerShell
Verify the installation:
Get-Module -Name PnP.PowerShell -ListAvailable
Step 2: Connect to SharePoint Online Admin Center
Since OneDrive is managed through SharePoint Online, you must first authenticate your session.
Run the following command:
$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive
Alternatively, if using App-based authentication, use:
$clientId = "your-client-id"
$tenantId = "your-tenant-id"
$clientSecret = "your-client-secret"
Connect-PnPOnline -Url $adminUrl -ClientId $clientId -ClientSecret $clientSecret -Tenant $tenantId
Once connected, you can proceed with creating OneDrive sites.
Step 3: Create a New OneDrive Site for a User
Each user in Microsoft 365 has a personal OneDrive URL. To create a OneDrive site for a user, use:
$userEmail = "user@yourdomain.com"
Request-PnPProvisionOneDriveForBusiness -User $userEmail
Write-Host "OneDrive site provisioning requested for $userEmail"
This command ensures that OneDrive is created for the user if it does not already exist.
Step 4: Verify OneDrive Site Creation
To check if the OneDrive site was successfully created:
$userEmail = "user@yourdomain.com"
$oneDriveSiteUrl = "https://yourtenant-my.sharepoint.com/personal/" + ($userEmail -replace "@", "_") + "/"
Get-PnPTenantSite -Url $oneDriveSiteUrl
If the site exists, it will return the site details, including storage quota and owner.
Step 5: Bulk Create OneDrive Sites for Multiple Users
To automate OneDrive creation for multiple users, create a CSV file (e.g., users.csv
) containing user emails:
Email
user1@yourdomain.com
user2@yourdomain.com
user3@yourdomain.com
Use the following PowerShell script to provision OneDrive sites for all users in the CSV file:
# Connect to SharePoint Online Admin Center
$adminUrl = "https://yourtenant-admin.sharepoint.com"
Connect-PnPOnline -Url $adminUrl -Scopes "Sites.FullControl.All" -Interactive
# Import the CSV file
$users = Import-Csv "C:\Scripts\users.csv"
# Loop through each user and request OneDrive provisioning
foreach ($user in $users) {
Request-PnPProvisionOneDriveForBusiness -User $user.Email
Write-Host "OneDrive provisioning requested for: $($user.Email)"
}
Write-Host "OneDrive site creation completed for all users."
Step 6: Automate and Schedule OneDrive Creation
To automate this process, save the script as “Create-OneDrive.ps1” and schedule it to run periodically.
1. Open Task Scheduler
- Click Start and search for Task Scheduler.
- Click Create Basic Task.
- Name it “OneDrive Site Provisioning”.
2. Set Trigger
- Choose Daily or another frequency as needed.
- Set the time when the script should run.
3. Set Action
- Select Start a Program.
- In Program/Script, enter: plaintextCopyEdit
powershell.exe
- In Arguments, enter: plaintextCopyEdit
-File "C:\Scripts\Create-OneDrive.ps1"
- Click Finish.
This will ensure OneDrive sites are created automatically for new users.
Step 7: Monitor OneDrive Site Status
To check the OneDrive creation status for all users:
$oneDriveSites = Get-PnPTenantSite -IncludeOneDriveSites | Where-Object { $_.Url -like "*-my.sharepoint.com/personal/*" }
$oneDriveSites | Select-Object Url, Owner, StorageUsageCurrent, StorageQuota | Format-Table -AutoSize
To export the list to CSV for reporting:
$oneDriveSites | Select-Object Url, Owner, StorageUsageCurrent, StorageQuota | Export-Csv -Path "C:\Reports\OneDrive_Sites.csv" -NoTypeInformation
Write-Host "Report saved to C:\Reports\OneDrive_Sites.csv"
Step 8: Assign Storage Quotas After Creation
Once the OneDrive sites are created, set storage limits:
$storageQuota = 102400 # 100 GB in MB
$warningLevel = 92160 # 90 GB in MB
foreach ($site in $oneDriveSites) {
Set-PnPTenantSite -Url $site.Url -StorageQuota $storageQuota -StorageQuotaWarningLevel $warningLevel
Write-Host "Storage limit set for: $($site.Url)"
}
This ensures new OneDrive sites follow storage policies.