Skip to content

/flags/teams/get-csonlineuser-filter

Get-CsOnlineUser -Filter: find Teams users by policy, phone number or account type

verified onenvironmentlocal · windows 10.0.26200 arm64 · GNU bash 5.3.15 (Git Bash)updated
tool
Microsoft Teams PowerShell
command
Get-CsOnlineUser
flag
-Filter
default
no filter: all users are returned (up to -ResultSize)

Get-CsOnlineUser -Filter "PROPERTY -eq 'value'" returns only the Teams users that match a condition, evaluated by the service, instead of downloading every user and filtering with Where-Object. It is how Teams administrators find who has a given policy, phone number, voice setting or account type. The reference: "The Filter parameter uses the same filtering syntax as the Where-Object cmdlet." The cmdlet is in the MicrosoftTeams module and needs Connect-MicrosoftTeams.

powershell
Connect-MicrosoftTeams
Get-CsOnlineUser -Filter "EnterpriseVoiceEnabled -eq `$true" | Select-Object DisplayName, LineUri
Get-CsOnlineUser -Filter {LineURI -eq "tel:+14255550100"}
Get-CsOnlineUser -Filter "FeatureTypes -Contains 'PhoneSystem'"
Get-CsOnlineUser -Filter {UserPrincipalName -like "test*"} -ResultSize 50

Syntax: strings and script blocks

The parameter is a string. A script block {...} is converted to its text, so both forms work, but they quote differently: inside double quotes, escape $ as `$true; inside single quotes or a script block, write $true. Checked offline in MicrosoftTeams 8.0.0 (Windows PowerShell 5.1):

powershell
Import-Module MicrosoftTeams
$c = Get-Command Get-CsOnlineUser
"Filter type: " + $c.Parameters['Filter'].ParameterType.Name
"AccountType values: " + ([enum]::GetNames($c.Parameters['AccountType'].ParameterType) -join ', ')
try { Get-CsOnlineUser -Filter {AccountType -ne 'Guest'} -ErrorAction Stop } catch { $_.Exception.Message }

Output

Filter type: String
AccountType values: User, ResourceAccount, Guest, SfbOnPremUser, IneligibleUser, Unknown
Session is not established, run Connect-MicrosoftTeams before requesting access token

The last line shows that the filter is sent to the service, which requires a session: without one the cmdlet stops with Session is not established, run Connect-MicrosoftTeams before requesting access token.

Useful filters from the reference

FilterFinds
{AssignedPlan -like "MCO"}users with a Teams Phone / Skype plan
"EnterpriseVoiceEnabled -eq `$True"users enabled for Enterprise Voice
{LineURI -eq "tel:+1234"}the user with that number
"FeatureTypes -Contains 'PhoneSystem'"users with the Phone System feature (module 4.0.0 and later)
{AccountType -ne 'IneligibleUser'}excludes users without Teams licenses
{SoftDeletionTimestamp -eq $null}excludes soft-deleted users
{WhenChanged -gt "1/25/2022 11:59:59 PM"}recently changed users

Sort with -Sort (alias -OrderBy), for example -OrderBy "DisplayName asc"; the reference notes that sorting on some attributes like LineURI can be case-sensitive.

Pitfalls

  • Check the attribute first. The reference documents filtering details per attribute (for example FeatureTypes only with -Contains, from module 4.0.0). For an attribute it does not mention, test on a small -ResultSize or filter the output with Where-Object.
  • Renamed attributes. Module 3.0.0 renamed several properties (ObjectId to Identity, Enabled to IsSipEnabled, FirstName to GivenName). Old scripts filtering on the old names need updating.
  • Result size. -ResultSize limits how many users come back; set it while you test a filter.
  • Phone numbers. Assignment is done with Set-CsPhoneNumberAssignment; a missing -PhoneNumberType there causes its own error.
  • Policies. To find who has a meeting policy and change it, see Grant-CsTeamsMeetingPolicy.

FAQ

How do I find users with a specific phone number?

Get-CsOnlineUser -Filter {LineURI -eq "tel:+14255550100"}. The reference shows the tel: form, the form with an extension (tel:+1234,ext:) and the bare number as separate examples, so try the format your numbers were assigned with.

How do I exclude guests and ineligible users?

Filter on AccountType, for example Get-CsOnlineUser -Filter {AccountType -ne 'Guest'}, or use the -AccountType parameter (User, ResourceAccount, Guest, SfbOnPremUser, IneligibleUser, Unknown) to select one type.

Why is a property empty when I use -Filter?

The reference lists attributes that are temporarily unavailable in the output when you use -Filter or omit -Identity, such as WhenChanged and CountryAbbreviation. Query that user with -Identity to see them.

Sources