Entra / Microsoft 365 · SharePoint & OneDrive
Update one drive group members
Sample script that uses group membership to assign OneDrive for Business storage quotas from a CSV allocation file.
Connect & set up
Run these once per session. All scopes are read-only unless the script makes changes.
Connect-MgGraph -Scopes Directory.Read.All, Group.Read.All# Read in allocated sizes for different group members# The CSV file used should have columns for group, groupid, and allocation (in megabytes)
Run it
The main script. Copy it, or download the .ps1 and run it from your console.
[array]$Modules = Get-ModuleIf ("Microsoft.Online.Sharepoint.PowerShell" -notin $Modules.Name) {Write-Host "Please connect to the SharePoint Online management module and restart the script."; break}# Connect to the Microsoft GraphConnect-MgGraph -Scopes Directory.Read.All, Group.Read.All# Read in allocated sizes for different group members# The CSV file used should have columns for group, groupid, and allocation (in megabytes)If (Test-Path "c:\temp\OneDriveAllocations.csv") {[array]$OneDriveAllocations = Import-CSV -Path c:\temp\OneDriveAllocations.csv} Else {Write-Host "Can't find group information to process - exiting" ; break}# Figure out the service domain and what this means for the root of OneDrive account URLs$ServiceDomain = (Get-MgOrganization).verifiedDomains | Where-Object {$_.IsInitial -eq $True} | Select-Object -ExpandProperty Name$OneDriveDomain =("https://{0}-my.sharepoint.com/personal/" -f $ServiceDomain.Split(".")[0])$Report = [System.Collections.Generic.List[Object]]::new()ForEach ($Group in $OneDriveAllocations) {# Get group members and extract their user principal names[array]$GroupMembers = Get-MgGroupMember -GroupId $Group.GroupId$GroupMemberUPN = $GroupMembers.additionalProperties.userPrincipalName# Calculate the quota allocation for group members (in megabytes)[int]$NewAllocation = $Group.Allocation; $NewAllocation = $NewAllocation*1024ForEach ($Member in $GroupMemberUPN) {# Figure out the OneDrive site URL for the user's account$MemberDomain = $Member.Replace(".","_")$MemberDomain = $MemberDomain.Replace("@", "_")$OneDriveSiteURL = $OneDriveDomain + $MemberDomain$CurrentQuotaGB = ((Get-SPOSite -Identity $OneDriveSiteURL).StorageQuota/1024)If ($CurrentQuotaGB -lt $Group.Allocation) {# Current allocation is less than the quota assigned to the group, so increase it to the group allocationWrite-Host ("Updating OneDrive storage allocation for account {0} to {1}" -f $Member, $Group.Allocation)Set-SPOSite -Identity $OneDriveSiteURL -StorageQuota $NewAllocation$ReportLine = [PSCustomObject]@{Guest = $MemberOneDrive = $OneDriveSiteURL"Old Quota" = $CurrentQuotaGB"New Quota" = $Group.Allocation }$Report.Add($ReportLine)} #End update quota} #End members of a group} #End all groups# Show what we did$Report | Out-GridView
Attribution
Author
Office365itpros