It may be necessary to check the user’s presence status (Online/Away/Busy) in Microsoft Teams before performing a particular action for some integration scripts. Let’s have a look at how to get and change a user’s status in Teams using the Microsoft Graph API and PowerShell.
Get Teams User Presence Status via PowerShell
If the Microsoft.Graph module is installed on your computer, connect to your tenant using your account:
Connect-MgGraph -Scopes Presence.Read.All,User.Read.All
Install-Module Microsoft.Graph -Scope AllUsers
Specify the UPN of the user whose status you want to get:
$TeamsUser = Get-MGUser -Userid [email protected]
Get-MgCommunicationPresence -PresenceId $TeamsUser.Id | select Activity, Availability
Available presence statuses:
- Availability: Available, Busy, Away, Offline
- Activity: Available, InACall, DoNotDisturb, InAConferenceCall, Away, OffWork, BeRightBack
You can list the Teams presence statuses for all users in the tenant:
$allUserStatus = @() $AllUsers=Get-MGUser foreach ($TeamUser in $AllUsers) { $TeamsStatus=Get-MgCommunicationPresence -PresenceId $TeamUser.Id $CurUserStatus = New-Object PSObject -Property @{ Activity=$TeamsStatus.Activity Availability=$TeamsStatus.Availability DisplayName=$TeamUser.DisplayName } $allUserStatus += $CurUserStatus } $allUserStatus
If you want to get a user status in Teams from a script, create a new app (Azure AD -> App registration) and delegate Presence.ReadWrite.All
permission to it (or Presence.Read
and Present.Read.All
permissions when running an app as a user).
Connect to your tenant and get a token:
$ApplicationID = "1111111-1111-1111-1111-11111111111" $TenatDomainName = "2222222-2222-2222-2222-222222222222" $AccessSecret = "3333333333333333333333333333333333333333333" $Body = @{ Grant_Type = "client_credentials" Scope = "https://graph.microsoft.com/.default" client_Id = $ApplicationID Client_Secret = $AccessSecret } $ConnectGraph = Invoke-RestMethod -Uri https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token -Method POST -Body $Body
When accessing Azure via API, you must provide a user ID (ObjectId, User Object GUID) instead of UserPrincipalName (UPN).
$UserId = "111111-2222-3333-4444-555555555" $headers = @{ "Authorization" = "Bearer $($tokenResponse.access_token)" "Content-type" = "application/json" } $ApiUrl = "https://graph.microsoft.com/v1.0/users/$UserId/presence" $Response = Invoke-RestMethod -Method GET -Uri $ApiUrl -ContentType "application\json" -Headers $headers -SkipHeaderValidation $Response
How to Change Teams Presence Status from PowerShell
You can use PowerShell and the Graph API to change a user’s presence status in Teams. Connect to Azure using the Graph API as shown above.
Use the following script to change a user status for 1 hour (PT1H):
$UserId = "111111-2222-3333-4444-555555555" $uri = "https://graph.microsoft.com/beta/users/$userid/presence/setPresence" $body = @" { "sessionId": "$ApplicationID", "availability": "Away", "activity": "Away", "expirationDuration": "PT1H" } "@ Invoke-RestMethod –Uri $uri –Method Post –Body $body –Headers $headers -ContentType "application/json"
Check that the user status in Teams has changed:
Get-MgCommunicationPresence -PresenceId $UserId