Forum Discussion
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 $teamColl.GroupId -Role Owner | Select-Object $teamColl.GroupId,$owner.UserId,$owner.User | export-csv -encoding unicode -NoTypeInformation -path E:\"Path"\Report_Components_Owners.csv -append
}
But at the beginning is fetching all the informations previously to write it down.
Is there a possibility to write it at the fetching or avoid to read 95k Teams at once and then write it.
Regards
JFM_12
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-MSFTMicrosoft
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 }
- JFM_12Iron ContributorHello
Thank you very much.
It worked.
Have a great day
JFM_12- Sayali-MSFTMicrosoftJFM_12-
Could you please share your valuable feedback via Microsoft Teams Developer Feedback link.
https://forms.office.com/Pages/ResponsePage.aspx?id=v4j5cvGGr0GRqy180BHbR7iCOpS5_b9Nqmwx43u5rtZUOThVR081SllSR05aSDQxQ0tUMDVPTVIxTi4u
- Use
- Sayali-MSFTMicrosoftJFM_12- Thanks for reporting your issue.
We will check this at our end and will get back to you.