Skip to content

Instantly share code, notes, and snippets.

@exactmike
Last active August 21, 2020 18:43
Show Gist options
  • Save exactmike/b24abe3441b39a78801f073e52d6d567 to your computer and use it in GitHub Desktop.
Save exactmike/b24abe3441b39a78801f073e52d6d567 to your computer and use it in GitHub Desktop.
Getting Azure AD Users by a Filterable Attribute
function Get-AzureADUserByAttribute
{
<#
.SYNOPSIS
Get Azure AD users by an arbitrary filterable attribute, such as MailNickName.
.DESCRIPTION
Get Azure AD users by an arbitrary filterable attribute, such as MailNickName.
.EXAMPLE
$list = Import-CSV .\TempTestUsers.csv
$list | Get-AzureADUserByAttribute -Attribute MailNickName -UserListAttribute ID
The csv file imported above has a column named ID which contains a value which will match a user's mailnickname in Azure AD.
The actual attribute being compared in Azure AD is MailNickName
This retrieves any matching users from the list from Azure AD where ID = MailNickName and passes them as output. Any errors that occur are also output.
.EXAMPLE
$list = Import-CSV .\TempTestUsers.csv
Get-AzureADUserByAttribute -Attribute MailNickName -UserListAttribute ID -UserList $list
The csv file imported above has a column named ID which contains a value which will match a user's mailnickname in Azure AD.
The actual attribute being compared in Azure AD is MailNickName
This retrieves any matching users from the list from Azure AD where ID = MailNickName and passes them as output. Any errors that occur are also output.
.EXAMPLE
$Mailboxes = @(Get-Mailbox -identity Mike)
Get-AzureADUserByAttribute -Attribute MailNickName -UserListAttribute Alias -UserList $Mailboxes
Retrieve mailbox(es) from Microsoft Exchange.
The actual attribute being compared in Azure AD is MailNickName. The Exchange Mailboxes have an attribute which should match (Alias).
This retrieves any matching users from the mailboxes from Azure AD where ID = MailNickName and passes them as output. Any errors that occur are also output.
.PARAMETER Attribute
The Azure AD Filterable Attribute that will be compared with the specified UserListAttribute. The comparison operator used is 'eq', an exact, not case sensitive match.
.PARAMETER UserListAttribute
The colunm/property/attribute from the UserList object(s) which will be compared to the Azure AD filterable Attribute. The comparison operator used is 'eq', an exact, not case sensitive match.
#>
[cmdletbinding()]
param(
[parameter(Mandatory, ValueFromPipeline)]
[psobject[]]$UserList #Any object representation of one or more users with attribute values you want to search Azure AD with.
,
[parameter()]
[string]$Attribute
,
[parameter()]
[string]$UserListAttribute
)
begin
{
if ($null -eq $(Get-AzureADCurrentSessionInfo))
{
throw('Run Connect-AzureAD before running this script')
}
}
process
{
foreach ($u in $UserList)
{
switch ([string]::IsNullOrWhiteSpace($UserListAttribute))
{
$true
{ Get-AzureADUser -Filter "$Attribute eq '$u'" }
$false
{ Get-AzureADUser -Filter "$Attribute eq '$($u.$UserListAttribute)'" }
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment