Forum Discussion

PS_Sanjay's avatar
PS_Sanjay
Copper Contributor
Jun 21, 2024

Need help with Powershell

I have created One Folder in each user's OneDrive named as K drive, and migrated data in that folder.

I need the size of that folder from each user's OneDrive through PowerShell.

powershelll 

  • DTB's avatar
    DTB
    Iron Contributor

    Hi PS_Sanjay,

     

    You can use PowerShell to retrieve the size of a specific folder in each user's OneDrive. Here's a step-by-step guide to help you achieve this.

    Prerequisites

    1. Admin Access: Ensure you have the necessary admin permissions to access each user's OneDrive.
    2. Microsoft Graph API Permissions: Set up and authenticate to Microsoft Graph API with the appropriate permissions (e.g., Sites.Read.All, Files.Read.All).

    Step-by-Step Guide

    1. Install Microsoft Graph PowerShell Module

    First, you need to install the Microsoft Graph PowerShell module if you haven't already.

     

    Install-Module Microsoft.Graph -Scope CurrentUser

     

     

    2. Connect to Microsoft Graph

    Authenticate to Microsoft Graph.

     

    Connect-MgGraph -Scopes "Sites.Read.All, Files.Read.All"

     

     

    3. Retrieve Folder Size for Each User

    You can use the following script to get the size of the "K drive" folder from each user's OneDrive.

     

    # List of user email addresses
    $userEmails = @("email address removed for privacy reasons", "email address removed for privacy reasons", "email address removed for privacy reasons") # Add more users as needed
    
    # Function to get folder size
    function Get-OneDriveFolderSize {
        param (
            [string]$userEmail,
            [string]$folderName
        )
        try {
            # Get the user's OneDrive site ID
            $userSite = Get-MgUserSite -UserId $userEmail
            $siteId = $userSite.Id
    
            # Get the Drive ID
            $drive = Get-MgSiteDrive -SiteId $siteId
            $driveId = $drive.Id
    
            # Get the folder by path
            $folder = Get-MgDriveItem -DriveId $driveId -ItemPath $folderName
            $folderId = $folder.Id
    
            # Get the folder size
            $folderSize = (Get-MgDriveItem -DriveId $driveId -ItemId $folderId -ExpandProperty "children").Children | Measure-Object -Property size -Sum
            return $folderSize.Sum
        }
        catch {
            Write-Error "Failed to get folder size for $userEmail: $_"
            return $null
        }
    }
    
    # Loop through each user and get the folder size
    foreach ($userEmail in $userEmails) {
        $folderSize = Get-OneDriveFolderSize -userEmail $userEmail -folderName "K drive"
        if ($folderSize -ne $null) {
            Write-Output "$userEmail: K drive folder size is $($folderSize / 1MB) MB"
        }
    }

     

     

    Explanation

    1. User Emails:

      • $userEmails contains the list of user email addresses. Update this array with the users you want to query.
    2. Get-OneDriveFolderSize Function:

      • This function takes a user's email and the folder name as parameters.
      • It retrieves the user's OneDrive site ID, the drive ID, and then the folder by its path.
      • It calculates the total size of the folder by summing the sizes of all items within the folder.
    3. Loop Through Users:

      • The script loops through each user email, calls the function to get the folder size, and outputs the result.

     

    Conclusion

    This script will help you retrieve the size of the "K drive" folder from each user's OneDrive. Make sure you have the necessary permissions and that the Microsoft Graph API is set up correctly. If you encounter any issues or need further assistance, feel free to ask.

     

    Please click Mark as Best Response & Like if my post helped you to solve your issue.

    This will help others to find the correct solution easily. It also closes the item.

    If the post was useful in other ways, please consider giving it Like.

    • ChetanNahel's avatar
      ChetanNahel
      Copper Contributor

      Thank you DTB but i tried this script and am getting this error

      At D:\temp\ODfoldersizetest\ODfoldersize.ps1:28 char:52
      + Write-Error "Failed to get folder size for $userEmail: $_"
      + ~~~~~~~~~~~
      Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.
      At D:\temp\ODfoldersizetest\ODfoldersize.ps1:37 char:23
      + Write-Output "$userEmail: Home data folder size is $($folderS ...
      + ~~~~~~~~~~~
      Variable reference is not valid. ':' was not followed by a valid variable name character. Consider using ${} to delimit the name.
      + CategoryInfo : ParserError: (:) [], ParseException
      + FullyQualifiedErrorId : InvalidVariableReferenceWithDrive

       

      Please provide your support on this

Resources