Update: OneDrive for Business Admin Tool

Update: OneDrive for Business Admin Tool

  •  
  •  
  •  
  •  
  •  
  •  
  •  
5 (100%) 2 votes

I was shocked to discover that I hadn’t updated the OneDrive for Business Admin Tool since January.  Shocked that I hadn’t had any good ideas for it. Shocked, I tell you.

And then, a requirement presented itself for a tenant-to-tenant migration: how big are all of the OneDrive sites?

As luck would have it, we have a way to figure it out, and, since the framework for enumerating the OD4B sites is already built inside the tool, it seemed like a good thing to add. Voila! The FolderSize parameter is born.

Once we have updated the SharePoint CSOM context to the user’s OneDrive site, we can use a CAML recursive query to pull back all of the files in the library.  From there, it’s just grabbing the file size and tallying it up. Easy peasy.

If ($docList.BaseType -eq "DocumentLibrary")
    {
        $TotalLibrarySize = 0
        $FileCount = 0
        $docSize = 0
        $camlQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
        $camlQuery.ViewXml = "<View Scope='RecursiveAll' />";
        $allLibraryItems = $doclist.GetItems($camlQuery)
        $ClientContextSource.Load($allLibraryItems)
        $ClientContextSource.ExecuteQuery()
        
        foreach ($item in $allLibraryItems)
        {
            $FileCount++
            if ($item.FileSystemObjectType -eq "File")
            {
                $file = $item.File
                $fItem = $file.ListItemAllFields
                $ClientContextSource.Load($file)
                $ClientContextSource.Load($fItem)
                
                $ClientContextSource.ExecuteQuery()
                $docSize = $fItem["File_x0020_Size"]
                [int]$TotalLibrarySize += $docSize
                
                # To output each file individually to screen for troubleshooting, uncomment
                #Write-Host "$($docList.Title), $($file.ServerRelativeUrl), $($fItem["FileLeafRef"].Split('.')[0]), $($docSize) KB"
            }
        }
        $TotalLibrarySize = $TotalLibrarySize/1048576

Easy peasy, hold the cheesy.

Since I’m writing this on a plane, I don’t have a lot of ability to get up and pace around and think about how to accomplish more bells and whistles with it, but at least in this maiden voyage of the tool, you can return the final version file size for every folder in every user’s OneDrive document library.

Since enumerating files in a document library requires permission to the library, you’ll most likely want to use the aptly-named GrantPermissions switch to do just that.  If you’ve used the tool previously, the structure and output will be familiar.  I mean, if it ain’t broke.

For the time being, the folder size data appears in the Notes column of the output.  At some point, I’ll revise the output structures to be a little more well-adapted, but this is not that time.

To run it:

.\OneDriveForBusinessAdmin.ps1 -FolderSize -Credential $cred -Tenant ems340903.onmicrosoft.com -GrantPermissions

As I’ve previously documented, GrantPermissions will use the value passed in Credential to grant rights.  You can override this settings by specifying an address with the GrantPermissionsTo switch.  You may want to revoke permissions to that account afterwards, which you can can do with the RemovePermissions switch.

To get the latest edition of this tool, hit up the TechNet Gallery at https://aka.ms/OneDriveAdmin.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.