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 Azure and Microsoft 365PowerShell

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
previous post
How to Increase Size of Disk Partition in Ubuntu
next post
Reset Root Password in VMware ESXi

Related Reading

View Windows Update History with PowerShell (CMD)

April 30, 2025

Uninstalling Windows Updates via CMD/PowerShell

April 18, 2025

Allowing Ping (ICMP Echo) Responses in Windows Firewall

April 15, 2025

How to Pause (Delay) Update Installation on Windows...

April 11, 2025

How to Write Logs to the Windows Event...

March 3, 2025

Leave a Comment Cancel Reply

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

Categories

  • Active Directory
  • Group Policies
  • Exchange Server
  • Microsoft 365
  • Azure
  • Windows 11
  • Windows 10
  • Windows Server 2022
  • Windows Server 2019
  • Windows Server 2016
  • PowerShell
  • VMware
  • Hyper-V
  • Linux
  • MS Office

Recent Posts

  • 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
  • AD Domain Join: Computer Account Re-use Blocked

    March 11, 2025
  • How to Write Logs to the Windows Event Viewer from PowerShell/CMD

    March 3, 2025
  • How to Hide (Block) a Specific Windows Update

    February 25, 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
  • Fix: Microsoft Outlook Search Not Working on Windows 10/11
  • Blank Sign-in Screen in Office 365 Apps (Outlook, Teams, etc.)
  • Removing Built-in Teams Chat in Windows 11
Footer Logo

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


Back To Top