powershell script to set different onedrive storage quota for security groups

MKHULEKO THABISO NHLAMBO 40 Reputation points
2025-02-11T08:36:47.9933333+00:00

Good day may you please assist me with a script if there's one that i can use to set storage limit for two different security groups (students and staff) on onedrive

SharePoint Development
SharePoint Development
SharePoint: A group of Microsoft Products and technologies used for sharing and managing content, knowledge, and applications.Development: The process of researching, productizing, and refining new or existing technologies.
3,304 questions
OneDrive Management
OneDrive Management
OneDrive: A Microsoft file hosting and synchronization service.Management: The act or process of organizing, handling, directing or controlling something.
1,352 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Emily Du-MSFT 50,326 Reputation points Microsoft Vendor
    2025-02-12T10:23:38.9333333+00:00

    Here are steps:

    1.In the SharePoint Online Management, install Microsoft.Graph module.

    Install-Module -Name Microsoft.Graph

    2.Find the group id of security group. Go to the Microsoft 365 admin center, navigate to "Teams & groups" -> "Active teams & groups", select the desired security group, and the group ID will be visible in the URL bar when you click on the group name.

    3.Create a csv file as following picture show.

    User's image

    4.Run following PowerShell. Replace the path with your file in the PowerShell.

    # Check that we're connected to SharePoint Online
    [array]$Modules = Get-Module
    If ("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 Graph
    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
    If (Test-Path "c:\OneDriveAllocations.csv") {
       [array]$OneDriveAllocations = Import-CSV -Path c:\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*1024
       ForEach ($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 allocation
            Write-Host ("Updating OneDrive storage allocation for account {0} to {1}" -f $Member, $Group.Allocation)
            Set-SPOSite -Identity $OneDriveSiteURL -StorageQuota $NewAllocation
            $ReportLine = [PSCustomObject]@{
              Guest       = $Member
              OneDrive    = $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
    

    5.Result:1


    If the answer is helpful, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.