Windows OS Hub
  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu
  • Home
  • About

Windows OS Hub

  • Windows Server
    • Windows Server 2022
    • Windows Server 2019
    • Windows Server 2016
    • Windows Server 2012 R2
    • Windows Server 2008 R2
    • SCCM
  • Active Directory
    • Active Directory Domain Services (AD DS)
    • Group Policies
  • Windows Clients
    • Windows 11
    • Windows 10
    • Windows 8
    • Windows 7
    • Windows XP
    • MS Office
    • Outlook
  • Virtualization
    • VMWare
    • Hyper-V
    • KVM
  • PowerShell
  • Exchange
  • Cloud
    • Azure
    • Microsoft 365
    • Office 365
  • Linux
    • CentOS
    • RHEL
    • Ubuntu

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

October 6, 2023 PowerShellTeams

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

How to Connect VPN Before Windows Logon

November 14, 2023

Send Emails with Microsoft Graph API and PowerShell

November 6, 2023

Zabbix: How to Get Data from PowerShell Scripts

October 27, 2023

Tracking Printer Usage with Windows Event Viewer Logs

October 19, 2023

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

October 15, 2023

Leave a Comment Cancel Reply

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

  • How to Connect VPN Before Windows Logon

    November 14, 2023
  • Removing Azure Arc Setup Feature on Windows Server 2022

    November 9, 2023
  • Using WPAD (Web Proxy Auto-Discovery Protocol) on Windows

    November 7, 2023
  • Send Emails with Microsoft Graph API and PowerShell

    November 6, 2023
  • Zabbix: How to Get Data from PowerShell Scripts

    October 27, 2023
  • Tracking Printer Usage with Windows Event Viewer Logs

    October 19, 2023
  • PowerShell: Configure Certificate-Based Authentication for Exchange Online (Azure)

    October 15, 2023
  • Reset Root Password in VMware ESXi

    October 12, 2023
  • How to Query and Change Teams User Presence Status with PowerShell

    October 8, 2023
  • How to Increase Size of Disk Partition in Ubuntu

    October 5, 2023

Follow us

  • Facebook
  • Twitter
  • Telegram
Popular Posts
  • Fix: Remote Desktop Licensing Mode is not Configured
  • Configuring Port Forwarding in Windows
  • Manage Windows Updates with PSWindowsUpdate PowerShell Module
  • Start Menu or Taskbar Search Not Working in Windows 10/11
  • How to Install Remote Server Administration Tools (RSAT) on Windows
  • How to Delete Old User Profiles in Windows
  • Adding Drivers into VMWare ESXi Installation Image
Footer Logo

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


Back To Top