Exporting All Phone Numbers in a Microsoft Teams Tenant via PowerShell QUICKLY with Export-CsAquiredPhoneNumber

One of the major issues introduced in the backed of the Get-CsPhoneNumberAssignment cmdlet is that it only pulls a maximum of 500 numbers at once, and is very SLOW in larger deployments (A tenant with 33K numbers takes about 50-55 minutes on average).
Microsoft recently introduced the Export-CsAquiredPhoneNumber cmdlet to solve this issue. What happens is when this command is invoked, a backend process is kicked off to generate a CSV of all phone numbers, their assignment status, provider, and more! This is almost exactly the same data that comes back with Get-CsPhoneNumberAssignment but sadly some newer parameters such as number tagging has not been introduced with this export. Because this is a backend-invoked export, this process exponentially less time. As in the example above, a tenant with 33K numbers completes this process in 1 minute. A tenant with 25 numbers completes this process in 7 seconds.
Returned options from this new command are as follows:

To make this easier and since the newer command ONLY generates a GUID to use for a download link, I have written a small bit of code to that will generate a download link to the CSV file:
#Kick off an export request on the Microsoft Side
$GUID = Export-CsAcquiredPhoneNumber
#Check the status of the export to move on to the next step
do
{
$Data = Get-CsExportAcquiredPhoneNumberStatus -OrderId $GUID
Write-Host "Link not Ready, Retrying" -ForegroundColor Yellow
Start-Sleep 2
}while ($Data.DownloadLink -notlike "*https:*")
#Once the file is avaliable, generate a download link
Write-Host "$($Data.DownloadLink)"
To expand on this for something useful in your own scripts, use the below code block to store the returned data in an array called $PhoneNumbers 🙂
#Set Some General Parameters for Downloading Files
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
$ProgressPreference = 'SilentlyContinue'
#Kick off an export request on the Microsoft Side
$GUID = Export-CsAcquiredPhoneNumber
#Check the status of the export to move on to the next step
do
{
$Data = Get-CsExportAcquiredPhoneNumberStatus -OrderId $GUID
Write-Host "Link not Ready, Retrying" -ForegroundColor Yellow
Start-Sleep 2
}while ($Data.DownloadLink -notlike "*https:*")
#Once the file is avaliable, download the file and store it in the $PhoneNumbers Array
$PhoneNumbers = Invoke-RestMethod -Uri $Data.DownloadLink | ConvertFrom-Csv

Leave a Reply