PowerShell Teams


In this post, we have a small tip for people who are using teams and want to create a helpdesk team and add all licenses users in Office 365 as members.

First, you need to install the Microsoft Teams PowerShell module.

Install-Module MicrosoftTeams -Scope CurrentUser

Once that is done, we can start creating the script to run.

We need to connect to Office 365 and Microsoft Teams. In most cases, these are the same credentials. You can collect them through a login screen or you can store it somewhere in a location and let Powershell retrieve it. That is what I am going to do.

$username = “admin@testaadjsol.onmicrosoft.com”

$secureString = Get-Content “C:\scripts\password.txt” | ConvertTo-SecureString
$myCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, $secureString

Connect-MicrosoftTeams -Credential $myCredentials
Connect-MsolService -Credential $myCredentials

Now we can create the team. We want to build in some safety measure to check that the team exists or not.

$group = Get-Team | ? { $_.DisplayName -like “HelpDesk”}
if (!$group)
$group = New-Team -DisplayName “HelpDesk” -AccessType Private
write-host “Group already HelpDesk exists”

Next step is to retrieve the licensed users

$users = Get-MsolUser -All| ? {$_.IsLicensed -eq $true}

Now we are going to use the $users collection to add them to the team

foreach ($user in $users)
if (Get-TeamUser -GroupId $group.GroupId | ? {$_.User -eq $user.UserPrincipalName})
write-host “$($user.UserPrincipalName) user already exists”
Add-TeamUser -GroupId $group.GroupId -User $user.UserPrincipalName -Verbose -Role Member

Run it and admire your work. One thing to remember, not all changes are going to be immediately visible in your Team application. According to documentation, there can be a delay up to 1 hour (at least at the time this blog post was witten)

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 )

Google+ photo

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

Twitter picture

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

Facebook photo

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


Connecting to %s