I am wanting to modify 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 like:
@{Label = 'SMTP Address'; Expression = { ($_.proxyAddresses | Where-Object { $_ -like "*smtp*" }) -replace 'smtp:' -join ';' } }
Here it is my full script:
#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://learn.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
I'm not sure what you mean by normal SMTP protocol address.
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.
However, it's pretty straight forward to add logic to a calculated property. Hopefully this example can give you the groundwork.
It can be a little messy so I like to park the Hash table expression definition in a separate variable to later reference in the Select-Object
command:
...
$SMTPExpression =
@{
Label = 'SMTP Address'
Expression =
{
If( $_.UserPrincipalName -match 'company1,onmicrosoft.com$')
{
$_.ProxyAddresses.Where( { $_ -match '^smtp:' } ) -replace 'smtp:'
}
Else
{
# Add some value or expression here...
$_.ProxyAddresses.Where( { $_ -match '^smtp:' } )
}
}
}
$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, $SMTPExpression, AlternateEmailAddresses, UsageLocation, isLicensed, Licenses, PasswordNeverExpires, BlockCredential |
$allUsers | Out-GridView
I did change your where statement a little, again just trying to make it a little shorter and a couple of other minor things (slight reformat so I could work easier....).
I don't have an environment to test this in, but the strategy is solid.