Create mailbox rule in Powershell?

Keith Hampshire 96 Reputation points
2023-01-19T18:15:12.8133333+00:00

We are using O365 for email. How can I create a mailbox rule on a Shared mailbox using PowerShell? We are having an issue with certain outside senders emailing to our shared mailbox over and over again. I want to have those senders moved to the Junk Email folder. I do not want to create a mailflow rule that blocks it because we want to have record of it.

If I use the "New-Inboxrule" command I receive an error message stating "Cannot open mailbox".

Have do I move (via a rule) all incoming email coming from externaluser01 to the junkfolder on sharedmailbox01?

Microsoft Exchange Online
Microsoft Exchange Online Management
Microsoft Exchange Online Management
Microsoft Exchange Online: A Microsoft email and calendaring hosted service.Management: The act or process of organizing, handling, directing or controlling something.
4,338 questions
0 comments No comments
{count} votes

Accepted answer
  1. Amit Singh 4,866 Reputation points
    2023-01-20T05:23:39.8266667+00:00

    If you want access to all mailboxes, then use the below.

    Get-Mailbox -ResultSize unlimited -Filter {(RecipientTypeDetails -eq 'UserMailbox')} | Add-MailboxPermission -User "yoursmtpaddress" -AccessRights FullAccess -InheritanceType all -AutoMapping:$false

    Just to confirm, this command will give you full access on all mailboxes.

    To create the custom folder, you can follow the below article.

    https://social.technet.microsoft.com/wiki/contents/articles/14515.create-a-custom-root-folder-in-all-the-mailboxes-bulk-in-exchange-2010.aspx

    0 comments No comments

4 additional answers

Sort by: Most helpful
  1. Jordi Rojas 266 Reputation points
    2023-01-19T18:33:36.29+00:00

    You can use the New-InboxRule command in PowerShell to create a mailbox rule on a shared mailbox in Office 365. However, the New-InboxRule command requires the use of the -Mailbox parameter to specify the mailbox to which the rule should be applied.

    When you try to create a rule on a shared mailbox using New-InboxRule command, you receive "Cannot open mailbox" error, because the New-InboxRule command can't be used directly on a shared mailbox, you need to use the -Mailbox parameter to specify the mailbox to which the rule should be applied.

    You can use the below command to create a mailbox rule on a shared mailbox to move all incoming email coming from externaluser01 to the junkfolder on sharedmailbox01

    New-InboxRule -Name "Rule for Externaluser01" -Mailbox sharedmailbox01 -From externaluser01@example.com -MoveToFolder JunkEmail

    Make sure you have the correct permissions to create a rule on a shared mailbox, You need the "Organization Management" role or "Full Access" permission to the shared mailbox.

    You can also use the Get-MailboxPermission command to check the permissions on the shared mailbox, and Add-MailboxPermission command to add the necessary permissions if you don't have them.

    Get-MailboxPermission -Identity sharedmailbox01

    Add-MailboxPermission -Identity sharedmailbox01 -User <user> -AccessRights FullAccess

    Please note that the above commands should be executed on Exchange Online PowerShell and not on the local PowerShell.

    0 comments No comments

  2. Keith Hampshire 96 Reputation points
    2023-01-19T19:08:45.14+00:00

    Yes I did that, however I'm still receiving the error message about, "Cannot open mailbox".

    Here is the error message:

    PS C:\Windows\system32> New-InboxRule -Name "movetojunkemail" -Mailbox sharedmailbox01 -From externaluser01@example.com -MoveToFolder "sharedmailbox01:\Junk Email" -StopProcessingRules $True
    
    Cannot open mailbox. Server = DM8PR09MB7432.namprd09.prod.outlook.com, user = /o=ExchangeLabs/ou=Exchange Administrative Group (FYDIBOHF23SPDLT)/cn=Recipients/cn=f190195534dc458eae3893ba14379752-, maiboxGuid =
    b7fd52e3-141f-4873-8851-5cc0249f0aaf
        + CategoryInfo          : NotSpecified: (:) [New-InboxRule], ConnectionFailedTransientException
        + FullyQualifiedErrorId : [Server=MN2PR09MB4715,RequestId=c5a5b747-9751-426c-9086-0bb3453dcc6c,TimeStamp=1/19/2023 6:59:56 PM] [FailureCategory=Cmdlet-ConnectionFailedTransientException] BA87807E,Microsoft.Exchange.Management.Recip
       ientTasks.NewInboxRule
        + PSComputerName        : outlook.office365.com
    

  3. Keith Hampshire 96 Reputation points
    2023-01-19T19:24:06.09+00:00

    Does it have to have a O365 license?

    0 comments No comments

  4. Keith Hampshire 96 Reputation points
    2023-01-20T14:57:01.6+00:00

    That was it! I needed to give myself access to that mailbox before I created the rule in powershell.

    I was thinking because I was a Global Admin that I did not need to do that but I was wrong.

    P.S. A shared mailbox doesn't need a license.

    0 comments No comments