Forum Discussion

Nikolaus Yates's avatar
Nikolaus Yates
Copper Contributor
Sep 09, 2022

Unable to select / get information from Microsoft 365 group

Hi, I want to use a script to create bulk Teams in Microsoft Teams.    In order to assign the members directly to the created teams, the group should be read from Excel in the script and assigned ...
  • LainRobertson's avatar
    LainRobertson
    Sep 10, 2022

    Nikolaus Yates 

     

    Okay, here's the basic template along with sample output from my environment.

     

    The key things to note are:

     

    1. The script caters to the different kinds of groups (unified, distribution and dynamic distribution);
    2. If you're wondering about the WindowsLiveID reference, that's because the Team commandlets use the userPrincipalName, not the PrimarySmtpAddress, and while these are often the same, they don't have to be, and I've seen many instances where poorly-configured AAD Connect environments produce users with an @onmicrosoft address instead of their organisational SMTP suffix;
    3. Unknown group types are skipped but you can easily extend the switch statement to include other RecipientType values (independent of whether they're group types or not);
    4. You can only list a team (the "Team name" column) once within the CSV file, which prevents you from providing multiple values for Group assigned to the same Team. I've done this based on your earlier answer about using multiple values for Member, where I've applied the same answer to the Group column.

     

    The sample output shows the script output boxed in orange. It's in the same format you provided in your script.

     

    Script template

     

    #CSV File Path. Change this location accordingly
    $filePath = "D:\Data\Temp\forum.csv"
    
    #read the input file
    if (Test-Path -Path $filePath)
    {
        $SuccessfullyCreated = 0;
    
        Import-Csv -Path $filePath |
            ForEach-Object {
                $Arguments = @{
                    DisplayName = $_.'Team name';
                    Description = $_.'Team description';
                    Owner = $_.Owner;
                    MailNickname = $_.MailNickname;
                    Template = $_.Template;
                }
    
                $MembershipGroupAddress = $_.Group;
            
                #create the team with specified parameters
                try
                {
                    $Team = New-Team @Arguments -ErrorAction:Stop;
    
                    switch (($MembershipGroup = Get-EXORecipient -Identity $MembershipGroupAddress -PropertySets Minimum -ErrorAction:Stop).RecipientTypeDetails)
                    {
                        "GroupMailbox" {
                            # This is our UnifiedGroup category.
                            Get-UnifiedGroupLinks -Identity ($MembershipGroup.ExternalDirectoryObjectId) -LinkType Members -ErrorAction:Stop |
                                Where-Object {
                                    $_.RecipientType.StartsWith("User")
                                } |
                                    ForEach-Object {
                                        Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop;
                                    }
                            break;
                        }
                        "MailUniversalDistributionGroup" {
                            # This is our traditional, manually-maintained distribution list category.
                            Get-DistributionGroupMember -Identity ($MembershipGroup.ExternalDirectoryObjectId) -ErrorAction:Stop |
                                Where-Object {
                                    $_.RecipientType.StartsWith("User")
                                } |
                                    ForEach-Object {
                                        Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop;
                                    }
                        break;
                        }
                        "DynamicDistributionGroup" {
                            # This is our dynamic distribution list category.
                            Get-DynamicDistributionGroupMember -Identity ($MembershipGroup.ExternalDirectoryObjectId) -ErrorAction:Stop |
                                Where-Object {
                                    $_.RecipientType.StartsWith("User")
                                } |
                                    ForEach-Object {
                                        Add-TeamUser -GroupId ($Team.GroupId) -User ($_.WindowsLiveID) -ErrorAction:Stop;
                                    }
                        break;
                        }
                        default {
                            # If we're in here, it's a type we aren't sure on how to handle, so we should skip it.
                            continue;
                        }
                    }
                }
                catch
                {
                    $Error[0];
                }
    
                $SuccessfullyCreated++;
                Write-Host "Team `"$($Team.DisplayName)`" created successfully...";
            }
        
        Write-Host $SuccessfullyCreated " teams were created" -ForegroundColor Green -BackgroundColor Black;
    }
    else
    {
        throw "CSV file not found. ($filePath)";
    }

     

     

    Sample output

     

    The commands and output below the orange-boxed output is only included to demonstrate that the teams were indeed created and had the nominated control group (from the "Group" column in the CSV) members added.

     

    Hopefully this helps you get started.

     

    Cheers,

    Lain

Resources