Forum Discussion
JFM_12
Aug 06, 2024Iron Contributor
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...
- 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:
- 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 }
- Use
Sayali-MSFT
Microsoft
Aug 07, 2024JFM_12- Thanks for reporting your issue.
We will check this at our end and will get back to you.
We will check this at our end and will get back to you.