New PowerShell Module Microsoft Teams 0.9.6 tutorial

The estimated reading time 6 minutes

Hi everyone,

some time passed since the last articles about Microsoft Teams and PowerShell integration. So there are some breaking news.  The teams module now finally can find all teams not only the team login user is member. Whoohoo 😉

One other big improvement is that you don’t need a MS Teams license anymore for administration. It is possible to do all this with an “teams admin”, but we will see later on.

So what can we do with this brand new feature? Now we can finally get an overview about teams in our organization.

First of all, please install the new module 0.9.6. See also PSGallery

You can do it in ISE or VSCode or normal Powershell Console. In this demo I choose the good old ISE.

Install-Module MicrosoftTeams -Verbose -Force

Of course after installation you can not use the module directly, so you have to import it in your PowerShell session. So quit easy just use the import-module cmdlet

Import-Module MicrosoftTeams -Verbose -Force

Powershell is importing the cmdlets and everything you need to start. Please check if you have the right version!

Get-Module MicrosoftTeams

How about administration rights in teams?

As mentioned above you do not need a “global administrator” to do something with PowerShell in teams. So let’s visit the AdminCenter of your O365 organization. I personally generated a brand new admin user especially for teams administration.

NOTE: Teams License is NOT required!

For detailed information about O365 Administration roles see this Link

After activation give azure some seconds, go back to PowerShell and connect to MSTeams with your new user (or any other user who has privileged rights).

the following code only works in ISE or pure PowerShell console

$tenant = (Connect-MicrosoftTeams)
$account = ($tenant.account)
$tenantdomain = ($tenant.TenantDomain)
$host.ui.RawUI.WindowTitle = "Windows Powershell remote connection to MSTeams Tenant: $tenantdomain with account $account "


For demonstration purposes I execute the this lines in pure PowerShell console, but it also works in ISE.

I’ll now switch back to my ISE and do the same here.

Finally we can do some cool stuff in here.

 

Using MS Teams PowerShell

If you use “Get-Team” all teams in your organization appear in the shell. But you only can see the groupid, displayname and description. Not that much information. Therefore I created a short script which treats all teams, users, owners, guests and shows the result in GridView (separated window)  see also the comments!

$userid = "user01@tenantname.onmicrosoft.com"
$Creds = Get-Credential $userid
Connect-MicrosoftTeams -Credential $Creds
#generate array for custom object
$teamsinfo = @()
#get all teams from organisation
$teams = get-team 
#find members, owner, guest
foreach($team in $teams){
  $displayname = ($team.DisplayName)
  $Description = ($team.Description)
  $groupid = $team.groupid
  $members = (Get-TeamUser -GroupId $groupid -Role Member).User
  $owner = (Get-TeamUser -GroupId $groupid -Role Owner).User
  $guests = (Get-TeamUser -GroupId $groupid -Role Guest).User
   #custom object for output
  $teamsinfo += [pscustomobject]@{
    DisplayName   = $displayname
    Owner = ("$owner")
    Members = ("$members")
    Guests = ("$guests")
    Description = ("$Description")
  }
}
#show teaminformation in OutGrid-View
$teamsinfo | Sort-Object DisplayName | Out-GridView -Title "All Office365 Groups created in MS Teams" 

Looks quite nice but I thought it would be nicer to have this information as an html report. Not that complicated.

#show teaminfo in html site
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6264A7;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
$htmlreport = $teamsinfo | ConvertTo-Html -Head $Header 
$htmlreport | Out-File .\teams.html
Invoke-Item .\teams.html

Not that bad, it may be useful to send that html report via mail to a MSTeam of administrators or something like this. So let’s do the job. In my lab I take O365 Server for relay but you can also use an local mail server.

NOTE: if you use exchange online the “FROM” User has to have an exchange mailbox (license issued)

$date = Get-Date
Send-MailMessage -From teams@it-koehler.com -To "General - sharepoint-demo <XXXXXXXX.it-koehler.com@emea.teams.ms>" -cc distributiongroup@it-koehler.com -Subject "MicrosoftTeamsReport $date" -BodyAsHtml "$htmlreport" -SmtpServer "smtp.office365.com"  -Credential $Creds -UseSsl -Port 587

Here is the reference to send-mailmessage

Multiple recipients is also possible by separating them with comma.

Where did I get the teams e-mail address? Open Teams and choose the channel you want to get the mail address.

After this we merge every piece of the script together and here is the complete script:

$userid = "teamsadmin01@it-koehler.com"
$Creds = Get-Credential $userid
Connect-MicrosoftTeams -Credential $Creds
#generate array for custom object
$teamsinfo = @()
#get all teams from organisation
$teams = get-team 
#find members, owner, guest
foreach($team in $teams){
  $displayname = ($team.DisplayName)
  $Description = ($team.Description)
  $groupid = $team.groupid
  $members = (Get-TeamUser -GroupId $groupid -Role Member).User
  $owner = (Get-TeamUser -GroupId $groupid -Role Owner).User
  $guests = (Get-TeamUser -GroupId $groupid -Role Guest).User
   #custom object for output
  $teamsinfo += [pscustomobject]@{
    DisplayName   = $displayname
    Owner = ("$owner")
    Members = ("$members")
    Guests = ("$guests")
    Description = ("$Description")
  }
}
#show teaminformation in OutGrid-View
$teamsinfo | Sort-Object DisplayName | Out-GridView -Title "All Office365 Groups created in MS Teams" 
#show teaminfo in html site
$Header = @"
<style>
TABLE {border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse;}
TH {border-width: 1px; padding: 3px; border-style: solid; border-color: black; background-color: #6264A7;}
TD {border-width: 1px; padding: 3px; border-style: solid; border-color: black;}
</style>
"@
$htmlreport = $teamsinfo | ConvertTo-Html -Head $Header 
$htmlreport | Out-File .\teams.html
Invoke-Item .\teams.html
$date = Get-Date
Send-MailMessage -From teamsadmin01@it-koehler.com -To "General - sharepoint-demo <XXXXXXXXXX@emea.teams.ms>" -cc XXXXXXXX@it-koehler.com -Subject "MicrosoftTeamsReport $date" -BodyAsHtml "$htmlreport" -SmtpServer "smtp.office365.com"  -Credential $Creds -UseSsl -Port 587

Of course there are som other great features you can do with PowerShell, but this tutorial  shows how to get some information. If you liked the article please click “helpful”. Thanks for reading and have a look at my other articles. Have fun.

Print Friendly, PDF & Email
Was this article helpful?
YesNo
0 0 votes
Article Rating
Subscribe
Notify of
guest
5 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
Saravanan
Saravanan
3 years ago

HI,

Is it possible to get the M365 Groups along with getting details like Teams name, owner & Guest.

trackback
4 years ago

[…] works, but now Teams Module was relaunched and can do quite more things. See my later article: https://blog.it-koehler.com/en/Archive/2074 NOTE: the following actions ONLY shows team groups created in teams, NO converted O365 groups are […]

christophe petit
christophe petit
4 years ago

Great Job !
Should be nice to get all O365 groups (not only teams one)
Anyway to do this ?