I need some help in modifying the Powershell to list users from Azure AD to display the information correctly in the SMTP Address Column.
The below script is correct, I just need to make some changes like below if possible:
If the UserPrincipalName value contains Company1.onmicrosoft.com, then SMTP Address column should show everything as it is, do not process anything.
If the UserPrincipalName value does NOT contain Company1.onmicrosoft.com, then SMTP Address column should NOT display the *@Company1.mail.onmicrosoft.com or *@Company1.onmicrosoft.com, so only the normal SMTP protocol address.
The Licenses column should NOT display the prefix Company1:EnterpriseE1, but just EnterpriseE1.
So far I can only display the ProxyAddresses with SMTP with the below line:
Code: Select all
@{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }
Code: Select all
#Import Module
If (!(Get-Module "*MSOnline*")) {Import-Module MSOnline}
If (!(Get-Module "*Exchange*")) {Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA + "\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse).FullName | ?{ $_ -notmatch "_none_" } | select -First 1)}
#Set admin UPN
$UPN = 'Global.Admin@domain.com'
#This connects to Azure Active Directory & Exchange Online
Connect-MsolService
$EXOSession = New-ExoPSSession -UserPrincipalName $UPN
Import-PSSession $EXOSession -DisableNameChecking -AllowClobber
$startsWith = @(
'Test'
'Sync_'
)
$endsWith = @(
'365'
'\$'
'svc'
'Sync'
'user'
)
$pattern = '^({0})|({1})$' -f $($startsWith -join '|'), $($endsWith -join '|')
# Member Outputs for Microsoft.Online.Administration.User based on https://docs.microsoft.com/en-us/powershell/module/msonline/get-msoluser?view=azureadps-1.0
$allUsers = @()
$allUsers = Get-MsolUser -All -EnabledFilter EnabledOnly | Where-Object {
($_.UserPrincipalName -notmatch $pattern) -and
($_.UserPrincipalName -notlike '*#EXT#*') -and
($_.DisplayName -notmatch 'Admin|Calendar|Room|Prod|Account|Fax|Team|Office|Test|User')
} | Select-Object FirstName, LastName, UserPrincipalName, @{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }, AlternateEmailAddresses, UsageLocation, isLicensed, Licenses, PasswordNeverExpires, BlockCredential
$allUsers | Out-GridView