Create Helpdesk Team and add all licensed users as members through PowerShell

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.

1
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.

1
2
3
4
5
6
7
$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.

1
2
3
4
5
6
7
8
9
$group = Get-Team | ? { $_.DisplayName -like "HelpDesk"}
if (!$group)
{
$group = New-Team -DisplayName "HelpDesk" -AccessType Private
}
else
{
write-host "Group already HelpDesk exists"
}

Next step is to retrieve the licensed users

1
2
3
4
5
6
7
8
9
10
11
12
13
$users = Get-MsolUser -All| ? {$_.IsLicensed -eq $true}</blockquote>
Now we are going to use the $users collection to add them to the team
<blockquote>foreach ($user in $users)
{
if (Get-TeamUser -GroupId $group.GroupId | ? {$_.User -eq $user.UserPrincipalName})
{
write-host "$($user.UserPrincipalName) user already exists"
}
else
{
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 written)

Advertisements

Leave a Reply