Forum Discussion

JFM_12's avatar
JFM_12
Iron Contributor
Aug 06, 2024
Solved

Powershell to get Owners of all Teams

Hello We have 95k Teams. I have a script that is running and works.   # Loop through the teams foreach($team in $teamColl) { # Get the team owners Get-TeamUser -GroupId $team...
  • Sayali-MSFT's avatar
    Aug 07, 2024

    JFM_12 -

    When dealing with a large number of Teams and performing operations like fetching and writing data, it's crucial to manage resources efficiently to avoid performance issues and excessive memory usage. To address your concern, you can modify your script to write data incrementally rather than storing all the data in memory before writing it out. Here's an optimized approach for handling this scenario:

    1. Use Export-Csv Efficiently: In PowerShell, Export-Csv can be used to append data incrementally. However, it might be more efficient to handle this using a stream writer for very large datasets.
      Document link-Export-Csv (Microsoft.PowerShell.Utility) - PowerShell | Microsoft Learn

    Updated Script

    Here’s an optimized version of your script that writes data incrementally:

    # Define the output file path
    $outputFilePath = "E:\Path\Report_Components_Owners.csv"
    
    # Initialize the CSV file with headers
    $header = "GroupId,UserId,User"
    $header | Out-File -FilePath $outputFilePath -Encoding Unicode -Force
    
    # Loop through the teams
    foreach ($team in $teamColl) {
        # Get the team owners
        $owners = Get-TeamUser -GroupId $team.GroupId -Role Owner
    
        # Prepare the data to be written
        $data = $owners | Select-Object @{Name="GroupId";Expression={$team.GroupId}}, UserId, User
    
        # Write data to CSV
        $data | Export-Csv -Path $outputFilePath -Encoding Unicode -NoTypeInformation -Append
    }
    

Resources