Use Microsoft Graph API with PowerShell – Part 4

Ok, we are now at the final Post in this Series. We have gone throught the following Parts

Part 1 – Authentication and Azure App – Use Microsoft Graph API with PowerShell – Part 1 » TechGuy
Part 2 – Oauth2.0 – Use Microsoft Graph API with PowerShell – Part 2 » TechGuy
Part 3 – First Powershell Script to get a Teams Lis and Walkthrough – Use Microsoft Graph API with PowerShell – Part 3 » TechGuy
Part 4 – this one – Use Microsoft Graph API with PowerShell – Part 4 » TechGuy

Now I am gonna show you some example, not much explanation, just Links, PowerShell, Screenshots and Results. Enjoy

Get a list of all Teams

Reference: List all teams in Microsoft Teams for an organization – Microsoft Graph | Microsoft Docs and List groups – Microsoft Graph beta | Microsoft Docs

PowerShell

$headers = @{
        "Authorization" = "Bearer $($tokenResponse.access_token)"
        "Content-type"  = "application/json"
    }

$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"  
$AllTeams = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value

Result

The Variable “$AllTeams” contains an Array of all Teams.

Get Channels from a Team

Reference: List channels – Microsoft Graph v1.0 | Microsoft Docs

Powershell


$TeamName="Marketing"

$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"  
$AllTeams = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value 

$Team=$AllTeams | Where-Object -Property Displayname -Value "Marketing" -eq

$URL = "https://graph.microsoft.com/v1.0/teams/$($Team.id)/channels"  
$AllChannels = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value 

Result

The Variable “$AllChannels” contains all Channels for the Team “Marketing”

Get Members from a Team

Reference: List members of team – Microsoft Graph v1.0 | Microsoft Docs

Powershell

$TeamName="Marketing"

$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"  
$AllTeams = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value 

$Team=$AllTeams | Where-Object -Property Displayname -Value $TeamName -eq

$URL = "https://graph.microsoft.com/v1.0/teams/$($Team.id)/members"  
$AllMembers = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value 

Result

The Variable “$AllMembers” contains all Members for the Team “Marketing”

Add a Member or Owner to a Team

Reference: Add member – Microsoft Graph v1.0 | Microsoft Docs

Powershell


$TeamName="Marketing"
$UserUPN="jasmine.hofmeister@au2mator.com"
$TeamRole="Member"

$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

$URL = "https://graph.microsoft.com/beta/groups?`$filter=resourceProvisioningOptions/Any(x:x eq 'Team')"  
$AllTeams = (Invoke-RestMethod -Headers $headers -Uri $URL -Method GET).value 

$Team=$AllTeams | Where-Object -Property Displayname -Value $TeamName -eq

if ($TeamRole -eq "Member")
{
    $URL = "https://graph.microsoft.com/v1.0/groups/$($Team.id)/members/`$ref"  
}
elseif ($TeamRole -eq "Owner")
{
    $URL = "https://graph.microsoft.com/v1.0/groups/$($Team.id)/owners/`$ref"  
}

$body = [ordered]@{
    "@odata.id" = "https://graph.microsoft.com/v1.0/users/$UserUPN"
}

$bodyJSON = $body | ConvertTo-Json

Invoke-RestMethod -Headers $headers -Uri $URL -Method POST -Body $bodyJSON 

Result

Create a New Team

Reference: Create team – Microsoft Graph v1.0 | Microsoft Docs and Get a user – Microsoft Graph v1.0 | Microsoft Docs

Powershell


$TeamName = "Sales"
$TeamDescription = "The best Sales Team"
$TeamVisibility = "Public"
$Owner_UserUPN = "michael.seidl@au2mator.com"



$headers = @{
    "Authorization" = "Bearer $($tokenResponse.access_token)"
    "Content-type"  = "application/json"
}

$URLOwnwer = "https://graph.microsoft.com/v1.0/users/$Owner_UserUPN"
$ResultOwner = Invoke-RestMethod -Headers $headers -Uri $URLOwnwer -Method Get

$BodyJsonTeam = @"
            {
               "template@odata.bind":"https://graph.microsoft.com/v1.0/teamsTemplates('standard')",
               "displayName":"$TeamName",
               "description":"$TeamDescription",
               "visibility":"$TeamVisibility",
               "members":[
                  {
                     "@odata.type":"#microsoft.graph.aadUserConversationMember",
                     "roles":[
                        "owner"
                     ],
                     "user@odata.bind":"https://graph.microsoft.com/v1.0/users/$($ResultOwner.id)"
                  }
               ]
            }
"@
$URLTeam = "https://graph.microsoft.com/v1.0/teams"
Invoke-RestMethod -Headers $headers -Uri $URLTeam -Method POST -Body $BodyJsonTeam -ResponseHeadersVariable responseHeaders

            
do {
    Start-Sleep -Seconds 30
    $URL = "https://graph.microsoft.com/v1.0$($responseHeaders.Location)"
    $TeamStatus = Invoke-RestMethod -Headers $headers -Uri $URL -Method GET
} until ($TeamStatus.Status -eq "succeeded")
            

Result

OK, that’s it, you got a lot of examples.

More to find on my GitHub Repo au2mator.com e.U, (github.com)

Michael Seidl

11 thoughts on “Use Microsoft Graph API with PowerShell – Part 4”

  1. I am trying to create new team.
    Invoke-RestMethod -Headers $headers -Uri $URLTeam -Method POST -Body $BodyJsonTeam -ResponseHeadersVariable responseHeaders

    I get error
    Invoke-RestMethod : A parameter cannot be found that matches parameter name ‘ResponseHeadersVariable’.

    Pease help me to fix this.

    Thank you

  2. Hi,

    I’m trying to get the photo of a user (“https://graph.microsoft.com/v1.0/users(‘***@***.***’)/photo/$value” ), I can list all users and get a single user, but for the photo it gives me 401 unauthorized. I checked that I have application permissions and User.Read.All, User.ReadWrite.All. What could be happening here?

    Thanks

  3. How can I add member to Teams private channel as owner using Graph API powershell

  4. Pingback: Create Microsoft Team with PowerShell and MS Graph API - TechGuy

  5. How do you add pagination to get items more than 100? By default Graph only pulls 100 items max.

Leave a Comment

Your email address will not be published. Required fields are marked *

*