Teams data extraction with Graph

Marco Perbellini 0 Reputation points
2025-02-11T18:08:51.1033333+00:00

Hi,

I need to convert the following command to Graph command because I need to export the full list of users with Teams service enabled.

Get-MsolUser -All | Where-Object { $_.Licenses.ServiceStatus.ServicePlan.ServiceName -match "TEAMS"} | Select-Object UserPrincipalName,ObjectId

Can you support me ? Thanks!

Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
13,264 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Vasil Michev 115.3K Reputation points MVP
    2025-02-12T07:31:47.72+00:00

    You can use the Get-MgUser cmdlet instead. And you can use server-side filtering to speed up the process a bit:

    Get-MgUser -All -Property Id,UserPrincipalName,AssignedPlans -Filter "assignedPlans/any(c:c/servicePlanId eq 57ff2da0-773e-42df-b2af-ffb7a2317929 and c/capabilityStatus eq 'Enabled')" -ConsistencyLevel eventual -CountVariable count | select Id,UserPrincipalName
    

    Do note the need for -ConsistencyLevel and -CountVariable parameters, as this is considered an advanced query.

    Also, there are some intricacies in the format used when you want to check whether a specific service/plan is enabled, as I explain here: https://www.michev.info/blog/post/6137/how-to-properly-filter-for-specific-enabled-services-via-the-graph-api-sdk

    0 comments No comments

  2. CarlZhao-MSFT 45,761 Reputation points
    2025-02-12T10:47:30.45+00:00

    Hi @Marco Perbellini

    I am not familiar with writing scripts, but I wrote a piece of code using Graph C# SDK to export a complete list of users who have enabled Teams service. Hope it can help you.

    var users = await graphClient.Users.GetAsync((requestConfiguration) =>
    {
        requestConfiguration.QueryParameters.Select = new string[] { "assignedPlans", "userPrincipalName", "id" };
    });
    
    try
    {
        if (users.Value != null)
        {       
            string folderPath = @"D:\MyFolder";
            string filePath = Path.Combine(folderPath, "users.csv");
          
            if (!Directory.Exists(folderPath))
            {
                Directory.CreateDirectory(folderPath);
            }
            using (var writer = new StreamWriter(filePath))
            {
                
                writer.WriteLine("ID,UserPrincipalName,AssignedPlans");
    
                foreach (var user in users.Value)
                {
                    if (user.AssignedPlans != null)
                    {
                        foreach (var assignedPlan in user.AssignedPlans)
                        {
                            if (assignedPlan.ServicePlanId.ToString() == "teams_servicePlan_id" && assignedPlan.CapabilityStatus == "Enabled")
                            {
                                //var assignedPlansJson = JsonConvert.SerializeObject(user.AssignedPlans, Formatting.Indented);
                                writer.WriteLine($"{user.Id},{user.UserPrincipalName},{user.AssignedPlans}");
                            }
                        }
                    }
                }
            }
        }
    }
    catch (ODataError odataError)
    {
        Console.WriteLine(odataError.Error.Code);
        Console.WriteLine(odataError.Error.Message);
    }
    

    User's image

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

    0 comments No comments

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.