Wednesday, May 22, 2019

Useful Teams PowerShell Commands

Back in the day, I compiled a shortish list of Lync/Skype for Business PowerShell commands that did some reasonably useful stuff. With my shift to Teams, I figure now is a good time to start compiling a list of semi-useful PowerShell scripts for Teams. I will add to this list as I think of them.

Getting Started

Before you run any of these scripts, you have to install the Teams PowerShell module. Thankfully, its very easy to do nowadays, as long as you're running PowerShell version 5.0 or higher (run $PSVersionTable.PSVersion to check). If you're not running PS v5.0 or higher, I recommend installing it. Once PS 5.x is installed, you simply run the following in an elevated PowerShell prompt to add the Teams module:
Install-Module -Name MicrosoftTeams -Repository PSGallery      

You then connect to your Teams tenant by running:
Connect-MicrosoftTeams

You will be prompted for admin credentials. Enter those, and you'll be all set!

Count All Teams Users in your Tenant

There isn't a direct way to count every Teams user across your tenant. However, you can get a list of users in a specific team. Thanks to PowerShell pipelining, you can string multiple commands together to get the desired result.
(Get-Team | Get-TeamUser | Sort-Object User -Unique).Count
If you want to exclude external accounts who you've granted guest access to Teams, modify the command accordingly:
(Get-Team | Get-TeamUser | Where {$_.User -notlike "*#EXT#*"} | Sort-Object User -Unique).Count
You can get a count of all Skype for Business (on-prem) and Teams users, excluding duplicates by running this from a Skype for Business PS prompt:

$SfBUsers = (Get-CsUser).SipAddress -Replace "sip:", ""Connect-MicrosoftTeams$TeamsUsers = (Get-Team | Get-TeamUser | Where {$_.User -notlike "*#EXT#*"} | Sort-Object User -Unique).User$CombinedUsers = $SfBUsers + $TeamsUsers | Sort-Object -Unique$CombinedUsers.Count