Forum Discussion

Melina's avatar
Melina
Copper Contributor
Jun 29, 2023

PowerShell script for reading team information

With a PowerShell script, the following information should be stored in a csv file:

- Team name
- Teams channels
- Team members
- Teams owner
- All files in a team channel incl. owner and with whom this file was shared

 

Is especially the last point possible to implement? who can help?

  • Thiraviam5316's avatar
    Thiraviam5316
    Brass Contributor

    Hi, Melina you can use the below Graph PowerShell script to read teams information to satisfy your needs.

     

    $teams = Get-MgGroup -Filter "resourceProvisioningOptions/Any(x:x eq 'Team')"
    foreach ($team in $teams){$teamName = $team.DisplayName
        $teamId = $team.Id
        Write-Host "Team Name: $teamName"
        $GroupOwner = get-mggroupowner -GroupId $teamId| Select AdditionalProperties
        $hashtable = $GroupOwner.AdditionalProperties
        Write-Host "Team Owner: $($hashtable.displayName) `n"
        $channels = Get-MgTeamChannel -TeamId $teamId
        foreach ($channel in $channels) {$channelName = $channel.DisplayName
            Write-Host "Channel Name: $channelName `n"
            $members = Get-MgTeamChannelMember -TeamId $teamId -ChannelId $channel.Id
            foreach ($member in $members) {
                Write-Host "Member: $($member.DisplayName)"
                }
             $messages = Get-MgTeamChannelMessage -TeamId $TeamId -ChannelId $channel.Id
             foreach ($message in $messages) {
                 foreach ($attachment in $message.Attachments) {
                        $fileName = $attachment.Name
                        $user = Get-MgUser -UserId $message.From.User.Id
                        Write-Host "File Name: $fileName"
                        Write-Host "Sent by: $($user.DisplayName) `n"            
                 }
             }
        }Write-Host "`n`n"
     }

     

    Further more, to get additional information on creating new team, modifying teams, managing channels and more using Graph PowerShell, refer the following blog: https://m365scripts.com/microsoft-teams/top-10-cmdlets-to-manage-teams-using-microsoft-graph-powershell/

     

    • Melina's avatar
      Melina
      Copper Contributor

      Thiraviam5316thank you!

      I am facing the next challenge:

       

      Can you help me further, which role or settings are necessary besides Global Admin? And where these roles must be assigned?

       

      • Thiraviam5316's avatar
        Thiraviam5316
        Brass Contributor

        Melina You must connect to the Microsoft Graph PowerShell with the required scopes for accessing the channel member: ChannelMember.Read.All, ChannelMember.ReadWrite.All
        For eg,
        Connect-MgGraph -Scopes "ChannelMember.Read.All, ChannelMember.ReadWrite.All"

        After Connecting to the Graph PowerShell, you can run the script to get the Teams report.

Resources