Powershell - Export inactive mailboxes Exchange 2010 - powershell

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"

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.

Problem with resultsize - A parameter cannot be found that matches parameter name 'resultsize'

I want to get office365 mailbox list which Last logon was more than 180 days? and export their .pst files to another location?
Wrote this script
But i am receiving error like - A parameter cannot be found that matches parameter name 'resultsize'
cant find where is the problem...
Get-MailboxStatistics -resultsize unlimited |
Where-Object {$_.LastLogonTime -lt (get-date).AddDays(-180)} |
Format-Table displayName,lastlogontime,lastloggedonuseraccount,servername
Get-MailboxStatistics has no -ResultSize argument. You should retrieve the mailboxes with Get-Mailbox and pipe results to get the statistics:
Get-Mailbox -ResultSize Unlimited | Get-MailboxStatistics

Powershell script to list all users that have message delivery restrictions in exchange 2010

Get-Mailbox -Filter {AcceptMessagesOnlyFrom -ne $Null}
This code only return few users and it shows ProhibitSendQuota,
I would like to return all users that have message delivery restrictions configured to accept messages from and reject messages from.
*Additional info: the users are located in sample.net/sample DIV OU/IDM_Users
Looking back through my Exchange Scripts, I have this for getting Delivery Restrictions:
Get-Mailbox –ResultSize Unlimited |
Where-Object {$_.AcceptMessagesOnlyFromSendersOrMembers –ne "" –or $_.RejectMessagesFromSendersOrMembers –ne ""} |
Select Name,Alias,AcceptMessagesOnlyFromSendersOrMembers,RejectMessagesFromSendersOrMembers
I'm not able to test it as I don't use Exchange anymore, but it should still be correct.
I managed to solve it by using the following query.
Get-Mailbox -ResultSize Unlimited -OrganizationalUnit "OU=SAMPLE DIV OU ,DC=SAMPLE,DC=NET" |
where-object {$_.AcceptMessagesOnlyFromSendersOrMembers –ne “” –or $_.RejectMessagesFromSendersOrMembers –ne “”} |
select Name, alias, AccpetMessagesOnlyFromSendersOrMembers, RejectMessagesFromSendersOrMembers
The Get-Mailbox cmdlet has a -OrganizationalUnit parameter that I used to target the OU.

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 }.

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

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