Using Powershell to get Office 365 SMTP: email addresses - powershell

Found the exact script I need:
https://unlockpowershell.wordpress.com/2010/01/27/powershell-get-mailbox-display-smtp-addresses/
Get-Mailbox -ResultSize Unlimited |Select-Object DisplayName,ServerName,PrimarySmtpAddress, #{Name=“EmailAddresses”;Expression={$_.EmailAddresses |Where-Object {$_.PrefixString -ceq “smtp”} | ForEach-Object {$_.SmtpAddress}}}
When I run this using Powershell5 against Office 365, "Email Addresses" is returned blank.
Any ideas?

This may have worked on Exchange 2007/2010, where the blog post says it was tested, but O365 mailboxes are a little different.
The EmailAddresses property of a mailbox object in O365 is an array list containing strings and they have no PrefixString property here. The issue is at this point
Where-Object {$_.PrefixString -ceq “smtp”}
Since PrefixString doesn't exist, you get blank results.
Instead, since the EmailAddresses array is just a bunch of strings, you can filter on those directly.
Get-Mailbox -ResultSize Unlimited | Select-Object DisplayName,ServerName,PrimarySmtpAddress, #{Name=“EmailAddresses”;Expression={$_.EmailAddresses | Where-Object {$_ -clike “smtp*”}}}

Related

Powershell - Get Unified Group with Mailaddress of the owners

I want to send a message to all people who are owners of a public MS Group. So my Code is:
Get-UnifiedGroup -ResultSize unlimited| Where-Object {($_.AccessType -eq "Public")} | Select-Object ManagedBy | Export-csv C:\Path
I can get the names, but how can I get the email address of the owners. I want to have it in one list.

Using filter to find email addresses matching a domain

I'm trying to find email addresses in O365 Exchange that matches a particular domain using PowerShell.
If I use:
Get-Recipient -ResultSize unlimited -filter '(PrimarySMTPAddress -like "*smith*")' | fl primarysmtpaddress
I get all the addresses that have the string
If I use:
Get-Recipient -ResultSize unlimited -filter '(PrimarySMTPAddress -like "*#domain*")' | fl primarysmtpaddress
I get no results.
It looks like nothing is matched after the #.
I want to use -filter rather than a where statement because it is so much faster.
I was able to reproduce your issue. It seems to be related to the the specific attribute you filtered on, "PrimarySMTPAddress".
I was able to get the filter statement to return results by changing it to leverage "EmailAddresses", another attribute the email address is stored in:
Get-Recipient -ResultSize unlimited -filter '(EmailAddresses -like "*#domain*")' | fl primarysmtpaddress
Something else I saw of note: the "filterable properties" documentation mentions avoiding using "PrimarySMTPAddress" for another reason that I didn't know of:
Don't use the PrimarySmtpAddress property; use the EmailAddresses property instead. Any filter that uses the PrimarySmtpAddress property will also search values in the EmailAddresses property. For example, if a mailbox has the primary email address dario#contoso.com, and the additional proxy addresses dario2#contoso.com and dario3#contoso.com, all of the following filters will return that mailbox in the result: "PrimarySmtpAddress -eq 'dario#contoso.com'", "PrimarySmtpAddress -eq 'dario2#contoso.com'", or "PrimarySmtpAddress -eq 'dario3#contoso.com'".
Source
https://learn.microsoft.com/en-us/powershell/exchange/filter-properties?view=exchange-ps

In users' e-mail addresses, remove all references to a domain

On our cloud-hosted O365 tenancy, I would like to run a PowerShell script which looks at every user and removes any reference to an e-mail domain I'm trying to get rid of.
As an example, if we have the following user:
Jimbob Goodemails
Jimbob.Goodemails#newdomain.com
Jimbob.Goodemails#olddomain.com
Jimbob.Goodemails#domain.onmicrosoft.com (the default O365 domain)
I would like to remove #olddomain.com addresses (in the above case, Jimbob.Goodemails#olddomain.com) from every user.
On most of these accounts, that domain will be set up as an alias - e.g. smtp:Jimbob.Goodemails#olddomain.com in the proxyAddresses field.
However, some users (far fewer, perhaps only 3 or 4) may have it as their main e-mail address (e.g. SMTP:Jimbob.Goodemails#olddomain.com in proxyAddresses but also as the mail and UPN attributes). These users can just use the default O365 e-mail address instead.
I have tried the following script, to no avail:
foreach($i in Get-Mailbox -ResultSize Unlimited) {
  $i.EmailAddresses |
    ?{$_.AddressString -like '*#olddomain.com'} | %{
      Set-Mailbox $i -EmailAddresses #{remove=$_}
    }
}
It sits for a while, then appears to finish, but nothing has changed.
I have used an Active Directory PowerShell script which does this for my AD-synchronised users, so I'm primarily looking to change Cloud users and groups.
It would also be useful to know how to search for such users too.
The Method:
Create an Array of your Current SMTP's:
$SMTPs = $Mailbox.EmailAddresses -split ","
Remove the Unwanted SMTP Addresses:
$SMTPs | ? {$_ -notmatch 'olddomain.com'}
Then Update The Mailbox with the filtered SMTPs:
Set-Mailbox $Mailbox -EmailAddresses $SMTPs
In your Example:
foreach ($i in Get-Mailbox -ResultSize Unlimited)
{
$SMTPs = $i.EmailAddresses -split "," | ? {$_ -notmatch 'olddomain.com'}
Set-Mailbox $i -EmailAddresses $SMTPs
}

Get SMTP address from a list of Names (Exchange 2013 powershell)

I'm trying to get SMTP address's for users who have OWAEnabled. I have what I think are two pieces that do what I want, but I can't figure out how to put them together. Ultimately it will output the SMTP address's to a CSV.
Get-CASMailbox -Filter{OWAEnabled -eq $true} | Select-Object Name
This gets me a list of user "Names" who have OWA enabled.
Get-Mailbox -ResultSize Unlimited | Select-Object PrimarySmtpAddress
This gets me Primary SMTP Address's
How do I put the two together? Sorry, i'm relatively new to PS.
Thanks for any help! :)
PrimarySMTPAddress is already a property of a CASMailbox object, so you just need to select it:
Get-CASMailbox -Filter{OWAEnabled -eq $true} | Select-Object Name,PrimarySMTPAddress
Most objects will have properties that aren't part of the default display properties, so they don't display by default.
Get-CasMailbox <identity> | format-list *
and you'll see all the available properties.

List all mailboxes that forward to a specific user

I have this script that lists all mailboxes that are forwarding email, however, I am curious if there would be a way to make it return all mailboxes that forward to a specific user. Basically I'm trying to find out every mailbox that forwards mail to "johndoe". Any help would be greatly appreciated! This is for exchange 2007 btw...
Here's the script so far:
$fwds = get-mailbox | Where-Object { $_.ForwardingAddress -ne $null }
| select Name, ForwardingAddress
foreach ($fwd in $fwds) {$fwd | add-member -membertype noteproperty
-name “ContactAddress” -value (get-contact $fwd.ForwardingAddress).WindowsEmailAddress}
$fwds
Exchange uses CanonicalName for the forwarding address, so you'll need to look that up from the user name. Since it could be a Mailbox, DL, or Contact, the easiest way I know of is to use Get-Recipient, and grab the Identity property.
$RecipientCN = (get-recipient johndoe).Identity
get-mailbox | Where-Object { $_.ForwardingAddress -eq $RecipientCN }
#mjolinor's version works, but is rather slow, since it loads all the mailboxes. On my system it took about 30 seconds to go through ~300 mailboxes.
This can be speed up by adding a filter to the Get-Mailbox command to only return ones that are actually being forwarded, like so:
$RecipientCN = (get-recipient johndoe).Identity
Get-Mailbox -ResultSize Unlimited -Filter {ForwardingAddress -ne $null} | Where-Object {$_.ForwardingAddress -eq $RecipientCN}
But wait, we can get even faster! Why not search for the correct user right in the filter? Probably because it's hard to get the syntax right, because using variables in -Filter gets confusing.
The trick is to use double quotes around the entire filter expression, and single quotes around the variable:
$RecipientCN = (get-recipient johndoe).Identity
Get-Mailbox -ResultSize Unlimited -Filter "ForwardingAddress -eq '$RecipientCN'"
This version returns the same results in 0.6s - about 50 times faster.