Windows OS Hub
  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux
  • Home
  • About

Windows OS Hub

  • Windows
    • Windows 11
    • Windows Server 2022
    • Windows 10
    • Windows Server 2019
    • Windows Server 2016
  • Microsoft
    • Active Directory (AD DS)
    • Group Policies (GPOs)
    • Exchange Server
    • Azure and Microsoft 365
    • Microsoft Office
  • Virtualization
    • VMware
    • Hyper-V
  • PowerShell
  • Linux

 Windows OS Hub / Azure and Microsoft 365 / How to Query and Change Teams User Presence Status with PowerShell

March 11, 2024

How to Query and Change Teams User Presence Status with PowerShell

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.

Contents:
  • Get Teams User Presence Status via PowerShell
  • How to Change Teams Presence Status from 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

If the Microsoft.Graph is not installed, you can install it as follows:

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

Get online presence status of MS Teams users with PowerShell

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

List Teams availability status for all tenant users

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 
Find out more about how to access Azure from PowerShell via Microsoft Graph API.

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 
You can also use this Azure app to read or send a message to the Teams chat using PowerShell.

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

PowerShell: update Teams user presence/status

0 comment
3
Facebook Twitter Google + Pinterest
Azure and Microsoft 365PowerShell
previous post
How to Increase Size of Disk Partition in Ubuntu
next post
Reset Root Password in VMware ESXi

Related Reading

How to Block Sender Domain or Email Address...

March 17, 2024

Automatic Outlook User Profile Configuration with ZeroConfigExchange

May 21, 2024

How to Manually Configure Exchange or Microsoft 365...

March 17, 2024

PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

March 17, 2024

Configuring Password Policy in Microsoft Entra ID

March 17, 2024

Removing Azure Arc Setup Feature on Windows Server...

November 9, 2023

Prevent Users from Creating New Groups in Microsoft...

March 17, 2024

Fix: “Something Went Wrong” Error When Installing Teams

March 13, 2024

Leave a Comment Cancel Reply

join us telegram channel https://t.me/woshub
Join WindowsHub Telegram channel to get the latest updates!

Recent Posts

  • Configuring Windows Protected Print Mode (WPP)

    May 19, 2025
  • Map a Network Drive over SSH (SSHFS) in Windows

    May 13, 2025
  • Configure NTP Time Source for Active Directory Domain

    May 6, 2025
  • Cannot Install Network Adapter Drivers on Windows Server

    April 29, 2025
  • Change BIOS from Legacy to UEFI without Reinstalling Windows

    April 21, 2025
  • How to Prefer IPv4 over IPv6 in Windows Networks

    April 9, 2025
  • Load Drivers from WinPE or Recovery CMD

    March 26, 2025
  • How to Block Common (Weak) Passwords in Active Directory

    March 25, 2025
  • Fix: The referenced assembly could not be found error (0x80073701) on Windows

    March 17, 2025
  • Exclude a Specific User or Computer from Group Policy

    March 12, 2025

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Outlook Keeps Asking for Password on Windows
  • Checking User Sign-in Logs in Entra ID (Microsoft 365)
  • How to Manually Configure Exchange or Microsoft 365 Account in Outlook 365/2019/2016
  • Search and Delete Emails from User Mailboxes on Exchange Server (Microsoft 365) with PowerShell
  • Removing Built-in Teams Chat in Windows 11
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Configuring Password Policy in Microsoft Entra ID
Footer Logo

@2014 - 2024 - Windows OS Hub. All about operating systems for sysadmins


Back To Top