Get Teams owner names - Custom action

  • Last update on January 25th, 2024

Table of Contents

If you want to retrieve Teams Owner names in a json string without the "" characters around the names or [] around the object. This is useful for including in emails to team owners.

In PowerShell:

$AllTeams = Get-Team
$TeamList = @()
foreach ($Team in $AllTeams)
{
$TeamGUID = $Team.GroupId.ToString()
$TeamName = $Team.DisplayName
write-host $TeamName
$TeamUsers = Get-TeamUser -GroupId $TeamGUID
$TeamOwner = ($TeamUsers | Where-Object {$_.Role -eq ‘Owner’}).User
$TeamList = $TeamList + [PSCustomObject]@{
TeamName = $TeamName;
TeamObjectID = $TeamGUID;
TeamOwners = $TeamOwner -join ‘; ‘;
}
}
$TeamList | export-csv c:\temp\TeamsData.csv -NoTypeInformation

In CoreView:

{
"id": "f2fc9d6f-50b8-49da-ad5a-4976cbc8109a",
"title": "Get Teams Owner Names",
"lastModified": "2022-02-09T11:56:13.3080000Z",
"target": "Teams",
"tags": [],
"vars": [],
"params": [
{
"name": "DisplayName",
"type": "string",
"isDefault": true
},
{
"name": "Id",
"type": "string",
"isDefault": false
}
],
"columns": {
"Id": "",
"DisplayName": ""
},
"version": 4,
"statement": "param ([string]$DisplayName, [string]$Id)\r\n\r\n$Id = \"bb4cdde2-eb41-40da-8531-7f4fd7556682\"\n$json = (Get-TeamUser -GroupId $Id -Role Owner | Select -ExpandProperty Name | ConvertTo-Json) -replace '\"', '' #-replace \",\",\"<br>\"\n\n$cleanjson = $json.Trim([char]0x005B, [char]0x005D)\n\nreturn $cleanjson"
}