Finding users in your Microsoft tenant with a specific UPN domain

If your tenant has multiple domains associated with it as many do, there may well be times you need to do some work with all users with a specific domain assigned as their UPN, for example updating their UPN or changing their email address (along with every other M365 option possible).

Here’s a very quick and easy PowerShell function that will get a list of users assigned to that domain. Simply run the function and specify the domain as a parameter – eg:

Get-UsersWithUPNDomain christurnbull.uk

would find all users in your tenant with @christurnbull.uk set in their UPN.

Function Get-UsersWithUPNDomain(){

	param (
	[Parameter(Mandatory=$true)][string]$domain
	)

	Get-MsolUser -All |? {$_.UserPrincipalName -like "*@$domain"}
}

Please note that as this function executes Get-MsolUser, you’ll obviously need to have installed the Msol module in PowerShell and connect to the Msol service before running this!

With over 20 years of working in the IT industry, I have a wide experience of many different areas of Information Technology and have specific interests in Cloud Solutions, Windows, Active Directory, and Infrastructure. I'm by no means a coder or programmer, and any snippets I post here work fine in my environment, but I give no assurances to how they may work for you.