PowerShell script sample - Create and assign a messaging policy

Use this PowerShell script to create a messaging policy in Microsoft Teams and assign it to users.

For more information about using this PowerShell script, see Quick start - Teams for Education.

This script uses the Grant-CsTeamsMessagingPolicy cmdlet which is in the Skype for Business Online PowerShell module. See Teams PowerShell overview to learn more about managing Teams using PowerShell.

Before you start

Download and install the Skype for Business Online PowerShell module, and then restart your computer if prompted.

To lean more, see Manage Skype for Business Online with Office 365 PowerShell.

Sample script

This script creates a messaging policy in Teams and assigns it to users.
Use this script to create a messaging policy and assign it to users in your organization.

$dataSetFilePath = "<csv file with user ids for newly provisioned students> "
 $dataSet = Import-Csv "$($dataSetFilePath)" -Header UserId –delimiter ","
 foreach($line in $dataSet)
    $userId = $line.UserId
    Write-Host $userId
    Grant-CsTeamsMessagingPolicy -PolicyName "<<PolicyName for a policy created with Chat Off>>" -Identity $userId



You can also assign a messaging policy directly to users at scale through a batch policy assignment or to a group that the users are members of. For more information see Assign policies to large sets of users in your school and Assign policies to your users in Teams.