Tag Archives: Stop emailing groups

Useful PowerShell Scripts for Managing Classes in Microsoft Teams

So, you have school data sync setup and all of your class teams have been generated in Microsoft Teams. Teachers are eager to start using it for extending the classroom or remote learning. Teachers then realise that students can do things that they were not aware of and request for some rights to be restricted.

Here is a list of useful PowerShell scripts to help you manage some of the most common issues that schools face.

  • – Allow teachers to delete student messages
  • – Stop students emailing the class group
  • – Disable chat for students

Allow teachers to delete student messages

It’s surprising that this is not enabled as standard. Owners in class teams cannot delete member messages unless a custom message policy is set.

Create a messaging policy in the Teams Admin centre

Create a new messaging policy and select “Owners can delete sent messages”

Create custom message policy in Teams
Owners can delete messages
Apply Custom Message Policy using PowerShell
 

This needs to be run as a global admin. The highlighted values should be changed to match the location of the users in AD and the custom message policy name.

Import-Module SkypeOnlineConnector
$userCredential = Get-Credential
$sfbSession = New-CsOnlineSession -Credential $userCredential
Import-PSSession $sfbSession
# add the OU of the teacher users below and update the highlighted code with your OU path and custom message policy
$teacherUsers = Get-ADUser -SearchBase “OU=XX,dc=XX,dc=XX,dc=XX,dc=gov” -Filter * -ResultSetSize 50000 | Select Name,SamAccountName 
 
foreach ($teacherUser in $teacherUsers)
    {
    write-host $teacherUser.Name
     Grant-CsTeamsMessagingPolicy -PolicyName "CustomStudentMessagingPolicy1" -Identity $teacherUser.Name
    }

Stop students emailing the class group

Once a student receives a welcome message into a group, they may reply back to it or find it in the address list and start a large group email.

In the script below connect to Microsoft Exchange PowerShell. You should update the variables with an AD security group for students to apply the policy to. To ensure you only apply this to the relevant teams, use the wildcard search to filter them. In this example we are assuming teams have been named in a format of SchoolCode-AcademicYear-ClassName so we can set the wildcard to only apply this setting to Teams starting with SCH-2019.

######Replace the following variables if necessary##########
$studentADSecurityGroup ="All Students"   #AD Group for all students
$wildcardsearch="SCH-2019*"                #Wildcard for Teams display name - Search for Teams beginning with ....  
###########################################################

$MyCredential = Get-Credential
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $MyCredential -Authentication Basic -AllowRedirection
Import-PSSession $Session -AllowClobber
$groups = Get-UnifiedGroup -ResultSize 20000 -SortBy DisplayName -Identity "$wildcardsearch" | Select DisplayName,WhenCreated,Id
 
foreach ($group in $groups)
{
    $teamName = $group.DisplayName
    Write-Host "restricting group emails on $teamName for $studentADSecurityGroup"
    Set-UnifiedGroup -Identity "$teamName" -RejectMessagesFromSendersOrMembers "$studentADSecurityGroup"
    Write-Host "Removing $teamName from the Global Address List"
}

Disable chat for students

Teams is a safe environment for students to chat, chats can be audited and monitored more closely than if they where to use WhatsApp or snapchat outside of the school systems. However, there are some situations where it might require turning off for safeguarding reasons.

Create message policy in Teams admin centre
Teams message policy

Click “Add” to create a new message policy and turn off the chat setting.

Turn off chat for students


Apply Custom Message Policy using PowerShell

This needs to be run as a global admin. The values should be changed to match the location of the users in AD and the custom message policy name.

Import-Module SkypeOnlineConnector
$userCredential = Get-Credential
$sfbSession = New-CsOnlineSession -Credential $userCredential
Import-PSSession $sfbSession
$studentUsers = Get-ADUser -SearchBase “OU=XX,dc=XX,dc=XX,dc=XX,dc=gov” -Filter * -ResultSetSize 50000 | Select Name,SamAccountName 
 
foreach ($studentUser in $studentUsers)
    {
    write-host $studentUser.Name
     Grant-CsTeamsMessagingPolicy -PolicyName "CustomStudentMessagingPolicy1" -Identity $studentUser.Name
    }