Scripts · PowerShell

Teams Phone System Snippets

PowerShell snippet to assign Teams Phone numbers in bulk from a CSV for Calling Plan, Operator Connect, or Direct Routing.

View on GitHub

What it is

A README with a PowerShell snippet, run with the MicrosoftTeams module, that reads a CSV and assigns phone numbers to users with Set-CsPhoneNumberAssignment. It handles Calling Plan numbers (with an emergency location ID), Operator Connect numbers, and Direct Routing numbers, and grants a voice routing policy for Direct Routing users.

Why use it

  • Assign phone numbers to many users at once instead of one at a time in the Teams admin center.
  • Use one CSV and loop for Calling Plan, Operator Connect, and Direct Routing number types.
  • Reports an error for rows with an invalid PhoneNumberType.

How to use it

  1. Install the MicrosoftTeams PowerShell module, run Connect-MicrosoftTeams, and update the module to the latest version.
  2. Create a CSV with UserPrincipalName, PhoneNumber, PhoneNumberType (CallingPlan, OperatorConnect, or DirectRouting), LocationId (required for CallingPlan), and VoiceRoutingPolicy (required for DirectRouting) columns.
  3. Adjust the CSV path and run the snippet from the README:
$assignphoneusers = Import-Csv "C:\path\to\assignphoneusers.csv"

foreach ($user in $assignphoneusers) {
    if ($user.PhoneNumberType -eq "CallingPlan") {
        Set-CsPhoneNumberAssignment -Identity $user.UserPrincipalName -PhoneNumber $user.PhoneNumber -PhoneNumberType $user.PhoneNumberType -LocationId $user.LocationId
    }
    # DirectRouting, OperatorConnect, and error handling branches are in the README
}

Prerequisites

  • MicrosoftTeams PowerShell module, latest version
  • CSV with the columns described above
  • Phone numbers available in the tenant and, for Calling Plan numbers, an emergency LocationId