Microsoft 365

MS Teams: Create Teams and add Members based on Group Membership

So, you are about to move to the cloud and now you want to create teams for your organization based on the group membership of your users. You also want to name the teams the way the groups are named. In this blog post, I’ll show you how to do that.

Let’s get started.

Connect to Microsoft Teams

First install the PowerShell Module for MSTeams and connect to your cloud tenant.

Install-Module MicrosoftTeams # if neccessary
Connect-MicrosoftTeams

If you see the screen above, move on.

Create Teams and add Members based on Group Membership

The following code gives you an example of how to create Teams and to name them the same as your groups the users are member of. All group users retrieved are automatically added to each new team.

# Create Teams and add members based on group membership
 
$groups = Get-ADGroup -Filter * -SearchBase "OU=groups,DC=domain,DC=com" |
Select-Object -ExpandProperty Name
 
foreach ($g in $groups) {
    $users = Get-ADGroupMember -Identity $g | 
    Select-Object -ExpandProperty SamAccountName
    $team = New-Team -DisplayName $g -Visibility Public 
 
    foreach ($u in $users) {
        $name = $u + '.' + $env:userdnsdomain.ToLower()
        Add-TeamUser -GroupId $team.groupid -User $name
}
}

PowerShell Automation completed successfully.

2 replies »

  1. Hi Patrick,

    I ran the script. The Teams are created but not populated. The ‘get-adgroupmember’ does not produce userprincipalname only samaccountname. Am i missing something?

    Get-ADGroupMember -Identity teamstestgroup1 | gm

    TypeName: Microsoft.ActiveDirectory.Management.ADPrincipal

    Name MemberType Definition
    —- ———- ———-
    Contains Method bool Contains(string propertyName)
    Equals Method bool Equals(System.Object obj)
    GetEnumerator Method System.Collections.IDictionaryEnumerator GetEnumerator()
    GetHashCode Method int GetHashCode()
    GetType Method type GetType()
    ToString Method string ToString()
    Item ParameterizedProperty Microsoft.ActiveDirectory.Management.ADPropertyValueCollection Item(string propertyName) {get;}
    distinguishedName Property System.String distinguishedName {get;set;}
    name Property System.String name {get;}
    objectClass Property System.String objectClass {get;set;}
    objectGUID Property System.Nullable`1[[System.Guid, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] objectGUID {get;set;}
    SamAccountName Property System.String SamAccountName {get;set;}
    SID Property System.Security.Principal.SecurityIdentifier SID {get;set;}

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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