Skip to content

Commit

Permalink
Merge pull request #624 from chandaniprajapati/dev-c-teams-cli
Browse files Browse the repository at this point in the history
Teams cli: Sample to get teams details and export it to CSV
  • Loading branch information
pkbullock authored Nov 22, 2023
2 parents 44976c1 + 05ffc17 commit 6598cf0
Show file tree
Hide file tree
Showing 4 changed files with 157 additions and 0 deletions.
105 changes: 105 additions & 0 deletions scripts/teams-export-details-using-teams-cli/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
---
plugin: add-to-gallery
---

# Fetch Teams Information Using Teams PowerShell And Export To CSV

## Summary

This script showcases the use of Microsoft Teams PowerShell commands to retrieve team details, including ID, name, owner, members, channels, etc., and then exports the information to a CSV file.

![Example Screenshot](assets/example.png)

## Implementation

Open Windows Powershell ISE
Create a new file and write a script
Install the Microsoft Teams module if it is not already installed using the `Install-Module MicrosoftTeams` command.

Now we will see all the steps which we required to achieve the solution:

1. Begin by establishing a connection to Microsoft Teams.
2. Retrieve the necessary details and store them in an array.
3. Export the collected data to a CSV file.

So at the end, our script will be like this,

# [MicrosoftTeams PowerShell](#tab/teamsps)

```powershell
$userName = "[email protected]"
$password = "********"
$secureStringPwd = $password | ConvertTo-SecureString -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential -ArgumentList $userName, $secureStringPwd
$dateTime = "_{0:MM_dd_yy}_{0:HH_mm_ss}" -f (Get-Date)
$basePath = "D:\Contributions\Scripts\Logs\"
$csvPath = $basePath + "\TeamsData" + $dateTime + ".csv"
$global:teamData = @()
Function Login() {
[cmdletbinding()]
param([parameter(Mandatory = $true, ValueFromPipeline = $true)] $creds)
#connect to teams
Write-Host "Connecting to Teams..." -f Yellow
Connect-MicrosoftTeams -Credential $creds
Write-Host "Connection successfully!..." -f Green
}
Function GetTeamsInformation {
try {
Write-Host "Getting teams details..." -ForegroundColor Yellow
$allTeams = Get-Team
Foreach ($team in $allTeams)
{
#Collect Team Data
$teamGroupId = $team.GroupId.ToString()
$teamChannels = Get-TeamChannel -GroupId $teamGroupId | Select-Object -Property DisplayName
$teamChannelsName = $teamChannels | Foreach {"$($_.DisplayName)"}
$joinedTeamChannels = [String]::Join('; ',$teamChannelsName)
$global:teamData += [PSCustomObject] @{
TeamName = $team.DisplayName
TeamID = $teamGroupId
MailAlias = $team.MailNickName
TeamType = $team.Visibility
TeamDescription = $team.Description
TeamChannels = $joinedTeamChannels
TeamOwners = (Get-TeamUser -GroupId $teamGroupId | Where {$_.Role -eq 'Owner'}).Name -join '; '
TeamMembers = (Get-TeamUser -GroupId $teamGroupId | Where {$_.Role -eq 'Member'}).Name -join '; '
}
}
Write-Host "Getting teams details successfully!..." -ForegroundColor Green
}
catch {
Write-Host "Error in getting teams information:" $_.Exception.Message -ForegroundColor Red
}
Write-Host "Exporting to CSV..." -ForegroundColor Yellow
$global:teamData | Export-Csv $csvPath -NoTypeInformation -Append
Write-Host "Exported to CSV successfully!..." -ForegroundColor Gree
}
Function StartProcessing {
Login($creds);
GetTeamsInformation
}
StartProcessing
```

[!INCLUDE [More about Microsoft Teams PowerShell](../../docfx/includes/MORE-TEAMSPS.md)]

***

## Contributors

| Author(s) |
|-----------|
| Chandani Prajapati (https://github.com/chandaniprajapati) |

[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/teams-export-details-using-teams-cli" aria-hidden="true" />
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 52 additions & 0 deletions scripts/teams-export-details-using-teams-cli/assets/sample.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
[
{
"name": "teams-export-details-using-teams-cli",
"source": "pnp",
"title": "Fetch Microsoft Teams Details And Export To CSV",
"shortDescription": "Retrieve team information such as team name, ID, owner, channels, etc., and export it to CSV using Microsoft Teams PowerShell.",
"url": "https://pnp.github.io/script-samples/teams-export-details-using-teams-cli/README.html",
"longDescription": [
""
],
"creationDateTime": "2023-11-16",
"updateDateTime": "2023-11-16",
"products": [
"Teams"
],
"metadata": [
{
"key": "MICROSOFTTEAMS-POWERSHELL",
"value": "3.0.0"
}
],
"categories": [
"Report",
"Connect-MicrosoftTeams",
"Get-Team",
"Get-TeamUser",
"Get-TeamChannel"
],
"thumbnails": [
{
"type": "image",
"order": 100,
"url": "https://raw.githubusercontent.com/pnp/script-samples/main/scripts/teams-export-details-using-teams-cli/assets/preview.png",
"alt": ""
}
],
"authors": [
{
"gitHubAccount": "chandaniprajapati",
"pictureUrl": "https://avatars.githubusercontent.com/u/52065929?v=4",
"name": "Chandani Prajapati"
}
],
"references": [
{
"name": "Want to learn more about Microsoft Teams PowerShell and the cmdlets",
"description": "Check out the Microsoft Teams PowerShell documentation site to get started and for the reference to the cmdlets.",
"url": "https://docs.microsoft.com/en-us/microsoftteams/teams-powershell-overview"
}
]
}
]

0 comments on commit 6598cf0

Please sign in to comment.