How to find all users on Exchange server from a particular OU? - powershell

I need to get a report of all users on an Exchange server who are located at one particular OU in Active Directory. Is there a way to do this with Powershell or VBS?
Thanks

Get-Mailbox -ResultSize Unlimited -OrganizationalUnit 'OU=test1,DC=domain,DC=com'

The PS cmdlet will work for versions of Exchange 2007 and later.
Here is how you'd extract the list and dump it in a text file. I've added the -IgnoreDefaultScope handle so that you're able to extract the list even though the OU in question is in a subdomain.
Get-Mailbox -IgnoreDefaultScope -ResultSize Unlimited -OrganizationalUnit 'OU=Staff Leavers,DC=my,DC=domain,DC=com' > C:\SysMgr\LeaverMailboxes.txt

Related

Exchange Powershell : Is it possible to differentiate on prem and cloud based distribution group?

I'm trying to create a script that must count the number of cloud and on prem based distribution group in a hybrid Exchange setup (on-prem + office 365).
I already did that with the users mailbox, it's possible to differentiate them by using the RecipientTypeDetails attribute. A "UserMailBox" represents an on prem user and a "MailUser" represents a cloud user.
Is there something similar for distribution group ? I didn't find the answer.
Thank your for your help, regards
I finally found the answer. Here are the cmdlets used by type of mailboxes :
"USER_ONPREM" = (Get-Mailbox -Resultsize Unlimited -RecipientTypeDetails "UserMailbox").Count
"USER_CLOUD" = (Get-Recipient -Resultsize Unlimited -RecipientTypeDetails "RemoteUserMailbox").Count
"SHARED_ONPREM" = (Get-Mailbox -Resultsize Unlimited -RecipientTypeDetails "SharedMailbox").Count
"SHARED_CLOUD" = (Get-Recipient -Resultsize Unlimited -RecipientTypeDetails "RemoteSharedMailbox").Count
"EQUIPMENT_ONPREM" = (Get-Mailbox -Resultsize Unlimited -RecipientTypeDetails "EquipmentMailbox").Count
"EQUIPMENT_CLOUD" = (Get-Recipient -Resultsize Unlimited -RecipientTypeDetails "RemoteEquipmentMailbox").Count
"ROOM_ONPREM" = (Get-Mailbox -Resultsize Unlimited -RecipientTypeDetails "RoomMailbox").Count
"ROOM_CLOUD" = (Get-Recipient -Resultsize Unlimited -RecipientTypeDetails "RemoteRoomMailbox").Count
"DL_ONPREM" = (Get-DistributionGroup -Resultsize Unlimited | where {$_.IsDirSynced -eq $False}).Count
"DL_CLOUD" = (Get-DistributionGroup -Resultsize Unlimited | where {$_.IsDirSynced -eq $True}).Count
To get on-prem mailboxes, it's just needed to use Get-Mailbox and filter by type.
To get remote mailboxes (i.e. mailboxes that are synced/migrated from onprem environment) it's possible to use Get-RemoteMailbox or Get-Recipient and filter by type with the suffix "Remote" added.
Finally to get "cloud only" mailboxes, it's only possible to retrieve them using an Exchange Online connection and from there we can verify which mailboxes are not synced with the attribute $_.IsDirSynced.
More infos : https://learn.microsoft.com/en-us/answers/questions/594318/exchange-powershell-is-it-possible-to-differentiat.html
When onPrem IsDirSynced indicates if the mailbox is synced to the cloud and when in the cloud IsDirSynced indicates if the mailbox is synced to on prem. Not very intuitive, but hey, it's Microsoft I guess.
I compared the result with what the ECP returns (The ECP classifies account as OnPrem or Office 365) and the numbers are correct.

Automate "Send me a copy of email I send to a group" via PowerShell

Referenced MS Article:
https://support.microsoft.com/en-us/office/i-m-not-receiving-a-copy-of-messages-i-send-to-a-group-in-my-inbox-07567cda-f5ce-4e52-b278-4c63dfdd6617
I am looking for any possibilities to globally update the setting found in the referenced link above.
I've looked through every admin portal and potential PS references with no luck.
I found this is a mailbox message configuration item. I'm using the ExchangeOnlineManagement module here, but it should be the same for other versions:
Connect-ExchangeOnline
# To see the current setting:
Get-Mailbox user#domain.com |
Get-MailboxMessageConfiguration |
Select EchoGroupMessageBackToSubscribedSender
# True|False
# To set for multiple users, get the list of mailbox objects
$mailboxes = Get-Mailbox ## Filter as needed, and check your -ResultSize too
# The Set command will take the whole list as input
$mailboxes | Where { -Not EchoGroupMessageBackToSubscribedSender } |
Set-MailboxMessageConfiguration -EchoGroupMessageBackToSubscribedSender $true

Is there a command to verify if MAPI is enabled for all users?

We have an organization in our Exchange services (2016) which has well above 200 users.
We need to verify if MAPI is enabled for each of them, and if not, who doesn't have it so we can enable it...
Is there a Powershell/ExchangeShell command which :
Lists all users who have MAPI enabled under an organization (we could then compare who's missing, it would already be much faster)?
List all users, and which APIs or protocols they have enabled (POP, IMAP, OWA, MAPI, etc.)?
Any of those would be of great help, so far I've only found a command in which you search for one user, but I don't want to have to do that for all 200 users...
This is fairly straight forward since most Exchange cmdlets allow piping directly into other Exchange cmdlets.
List all users, and which APIs or protocols they have enabled:
Get-CASMailbox -ResultSize unlimited
Possibly more useful to you is a list in CSV format since 200 users is tough to sort through in the PowerShell console. This will list more protocols and features, adjust as needed:
Get-CASMailbox -ResultSize unlimited | Select-Object -Property Identity, *Enabled | Export-Csv -NoTypeInformation -Path report.csv
List all users who have MAPI enabled:
Get-CASMailbox -ResultSize unlimited | where {$_.MapiEnabled -match "true"}
List all users who don't have MAPI enabled and enable it. Remove -WhatIf when ready to run it for real:
Get-CASMailbox -ResultSize unlimited | where {$_.MapiEnabled -match "False"} | Set-CASMailbox -MAPIEnabled $true -WhatIf
Note, this was run against an Exchange Online organization(where MAPI is enabled by default). You might need to use Get-CASMailbox -OrganizationalUnit <OrgName> or other filters to make sure you're only dealing with the correct mailboxes.

Add a trusted domain to everyone in exchange

Currently we have an HR email going out to the all of our staff. By default, the email gets sorted into the junk email for everyone.
I'd like to use the PowerShell cmdlet Set-MailboxJunkEmailConfiguration to add the sending domain as a trusted domain to everyone without having to set it individually for every person.
I know I need to use
Set-MailboxJunkEmailConfiguration DomainGoesHere -TrustedSendersAndDomains #{Add="whatever#abc123.com")
However I don't know what I need to add to target everyone in my domain.
Any help so I don't have to change this for hundreds of users by hand.
I ran the following command
Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains #{Add="mysafedomain.com"}
and get the following error
The Junk E-Mail configuration couldn't be set. The user needs to sign in to Outlook Web App before they can modify their Safe Senders and Recipients or Blocked Senders list.
+ CategoryInfo : NotSpecified: (545:Int32) [Set-MailboxJunkEmailConfiguration], DataSourceOperationException
+ FullyQualifiedErrorId : 47A2E998,Microsoft.Exchange.Management.StoreTasks.SetMailboxJunkEmailConfiguration
Here is the code that worked for me, thanks to Booga Roo
Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains #{Add="myTrustedDomain.com"} -ErrorAction SilentlyContinue
Thankfully Set-MailboxJunkEmailConfiguration accepts pipeline input for -Identity.
This should do it(replace the domain names, of course):
Get-Mailbox -ResultSize unlimited -RecipientTypeDetails UserMailbox | Set-MailboxJunkEmailConfiguration -TrustedSendersAndDomains #{Add="contoso.com","fabrikam.com"} -ErrorAction SilentlyContinue
Now with correct placement of the }.

Powershell - Export inactive mailboxes Exchange 2010

I'm looking to run a script like this and export to a CSV and sort by the last time the mailbox was used.
Basically we're trying to find mailboxes that haven't been used for more than 60 days. Once we load the list, we will run the script again except add a bit of code to export each mailbox to PST (I'm not sure how to do that portion, either)
The mailboxes were recently migrated from Exch 2007.
The mailboxes are backed up on a daily basis, so access time won't work.
I was looking at something like the below, but I get an error that it cannot get-mailboxstatistics from a mailbox on a server running version 8 while the script is running on version 14. This is likely because there are a large number of mailboxes we left on an Exchange 2007 server which are all terminated users. I would like to include these in the search results.
$xDays = 60
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox | Foreach-Object {
$si= Get-MailboxFolderStatistics $_ -IncludeOldestAndNewestItems -FolderScope SentItems
if($si.NewestItemReceivedDate -AND (New-TimeSpan $si.NewestItemReceivedDate.ToLocalTime()).Days -ge $xDays)
{
$_
}
}
You will probably need to run the script separately from 2007 and 2010 as it says.
Run it like this from the 2007 server:
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox -Server "2007 Server"
Like this from the 2010 server:
Get-Mailbox -ResultSize Unlimited -RecipientTypeDetails UserMailbox -Server "2010 Server"