How to Get Primary SMTP Address with Get-MsolUser via PowerShell
Question details
The user needs to retrieve a Microsoft 365 user's primary SMTP address, country, and other properties using PowerShell without losing the original user object context by expanding ProxyAddresses.

- Product
- Microsoft 365 / Exchange Online (PowerShell)
- Device & OS
- not provided
- Scenario
- Extracting detailed user properties including the primary email address via PowerShell scripting.
- Observed behavior
- Expanding ProxyAddresses typically drops other user object properties, making it difficult to export a clean list of users alongside their primary SMTP address.
Ensure you have the MSOnline PowerShell module installed and are connected to your Microsoft 365 tenant using Global Admin or Exchange Admin credentials.
Use Select-Object and Calculated Properties in PowerShell
Extract the primary SMTP address directly within a Select-Object command while keeping other necessary fields.
Instead of using the -ExpandProperty parameter which extracts only the array values, you can use a calculated property. This allows you to filter the ProxyAddresses array in-line for the primary address and attach it to the original user output.
Open PowerShell as an Administrator and connect to your Microsoft 365 tenant by running the command: Connect-MsolService.
Execute Get-MsolUser -All to retrieve the full list of users in your directory.
Pipe the results to Select-Object and specify the standard properties you want to keep, such as UserPrincipalName and Country.
Append a calculated property to filter the ProxyAddresses array using a regular expression match. The full command is: Get-MsolUser -All | Select-Object UserPrincipalName,Country,@{Name='PrimarySMTP';Expression={($_.ProxyAddresses | Where-Object {$_ -cmatch '^SMTP:'}) -replace '^SMTP:',''}}

Need a seamless alternative for your daily Office tasks?
While IT administration requires specialized tools like PowerShell, everyday document management shouldn't be complicated. WPS Office provides a free, lightweight, and highly compatible suite for Word, Excel, and PDF tasks, making it a perfect alternative to Microsoft Office.
- 1. Download the Installer: Visit the official WPS Office website and download the free installation package for your operating system.
- 2. Install WPS Office: Run the downloaded installer and follow the simple on-screen instructions to set up the software.
- 3. Open and Edit Documents: Launch WPS Office to instantly open, edit, and save your existing Microsoft Office files without formatting issues.

Frequently Asked Questions
Why does expanding ProxyAddresses hide other user properties?
When you use the Select-Object -ExpandProperty parameter in PowerShell, it outputs only the contents of the specified property array, dropping the parent object's context. Using a calculated property allows you to parse the array while keeping the parent object intact.
How do I distinguish between primary and alias email addresses in PowerShell?
In Microsoft 365, the ProxyAddresses attribute stores the primary email address with an uppercase 'SMTP:' prefix, while secondary aliases use a lowercase 'smtp:' prefix. You can use the '-cmatch' operator in PowerShell to filter specifically for the uppercase string.
Is the MSOnline PowerShell module deprecated?
Yes, Microsoft is gradually deprecating the MSOnline and AzureAD modules in favor of the Microsoft Graph PowerShell SDK. Consider migrating scripts from Get-MsolUser to Get-MgUser for long-term compatibility and support.




