EN 
30.11.2025 Ondřej WELCOME IN MY WORLD

This website is originally written in the Czech language. Most content is machine (AI) translated into English. The translation may not be exact and may contain errors.

Tento článek si můžete zobrazit v originální české verzi. You can view this article in the original Czech version.
Exchange - vytváření pravidel pomocí PowerShellu a OWA

Exchange - creating Inbox Rules using PowerShell and OWA

| Petr Bouška - Samuraj |
We can automatically process emails arriving in the mailbox (perform actions) using Inbox Rules. These rules are usually executed directly on the Exchange server, with some exceptions that require running in the Outlook application (client-only). Rules are most often managed in Microsoft Outlook. Here we will look at the possibility of managing rules for a Shared Mailbox using Outlook on the web (OWA). And the possibility of creating (bulk) rules using a cmdlet in the Exchange Management Shell (EMS).
displayed: 7 012x (1 428 CZ, 5 584 EN) | Comments [0]

Managing Rules for Shared Mailbox

If we use a shared mailbox (to which we have full rights assigned) in addition to our own mailbox, we can see it in Outlook. However, when setting up Rules & Alerts in Outlook, we can only work with rules for our own mailbox.

Note: The only solution would be to add the mailbox to Outlook as another account (which we must log into). Then we can switch accounts when setting up rules.

A simpler way to manage rules for a shared mailbox is through Outlook on the Web (OWA).

  • in the upper right corner, click on your account and select Open another mailbox
Outlook on the web (OWA) - Open another mailbox
  • enter the mailbox name, Search directory and Open (it will open in a new window)
  • in the shared mailbox, click on Settings (gear icon) in the upper right corner and then on Options
  • rules are managed under Mail - Automatic processing - Inbox and sweep rules
Outlook on the web (OWA) - Inbox and sweep rules

Configuring Rules Using PowerShell

As administrators, we can manage rules in mailboxes using Exchange Management Shell (EMS). We have several cmdlets available:

Limitations on the Number of Rules in a Mailbox

Exchange does not limit the number of rules that can be created in a mailbox. But the total size of rules in a mailbox can be maximum 256 kB. Unfortunately, we cannot find out how much space the rules currently occupy. If an error appears when creating a rule that it cannot be saved to Exchange, the capacity may be exhausted.

Mailboxes created on newer versions of Exchange have a rule size set to 256 kB. But previously the value was 64 kB. We can verify and possibly adjust the quota value for the mailbox.

[PS] C:\> Get-Mailbox bouska | FT Alias, RulesQuota

Alias  RulesQuota
-----  ----------
bouska 256 KB (262,144 bytes)

Creating a Rule

Rules are created easily using parameters that define conditions, actions, and exceptions. We can combine them in various ways.

Below is an example of a rule that forwards an email that contains a certain word in the recipient's address. We must name the rule and specify the mailbox to which it belongs. In some cases, it is necessary to specify addresses more precisely (than in the example) and enclose strings in quotation marks.

New-InboxRule -Name ForwardMail -Mailbox test-mailbox -RecipientAddressContainsWords petr -ForwardTo bouska `
 -StopProcessingRules $true 

Information About Existing Rules

List of rules for a mailbox.

Get-InboxRule -Mailbox test-mailbox

Detailed information about a specific rule.

Get-InboxRule -Mailbox test-mailbox -Identity ForwardMail | FL *

Deleting All Rules for a Mailbox

Get-InboxRule -Mailbox test-mailbox | Remove-InboxRule 

Practical Example - Mailbox with Many Addresses and Forwarding Rules

I received the following assignment. Ensure that emails sent to more than 200 special addresses arrive in one common mailbox that can be processed by multiple users. At the same time, some messages needed to be automatically forwarded based on the recipient's address.

Using Aliases vs. Distribution Groups

The common mailbox for collecting emails is ideally created as a shared mailbox. We can add aliases for individual addresses to this mailbox. We can use the PowerShell cmdlet (for example, load addresses from a CSV file).

Set-Mailbox test-mailbox@company.com -EmailAddresses @{Add=$alias}

Problem with Aliases

A major problem emerged. Outlook does not display the specific alias to which the email was sent. Only the name of the shared mailbox is visible. If we want to determine the target address, we must look in the message header.

Solution Using Distribution Groups

A possible solution is to create a separate distribution group for each email address, to which we add the shared mailbox as a member. Messages will thus be delivered to the shared mailbox and at the same time, it will be visible to which address they were sent.

Example of bulk creation of distribution groups from a CSV file:

$addresses=import-csv c:\addresses.csv
foreach($alias in $addresses) {
  $alias=$($alias.alias)
  $alias=$alias.Substring(0, $alias.IndexOf("@"))
  New-DistributionGroup -Name $alias -Alias $alias -Members test-mailbox@company.com -OrganizationalUnit "OU=Test,DC=company,DC=com" `
 -MemberJoinRestriction Closed -MemberDepartRestriction Closed -RequireSenderAuthenticationEnabled $false
  Set-DistributionGroup -Identity $alias -HiddenFromAddressListsEnabled $true 
}

Forwarding Rule - Filtering by Recipient Address

The second part of the assignment consisted of automatically forwarding messages based on the recipient's address. We must consider the 256 kB limit for rules in the mailbox. We need to create more than 200 rules. It is probably appropriate to use economical notation to make each rule as small as possible. For example, a short rule name or search string.

For filtering messages, we can use the condition RecipientAddressContainsWords - with specific words in the recipient's addresses. The rule applies to messages with the specified word in the recipient's address. In practice, the rule on a shared mailbox works correctly for distribution group addresses.

Note: We can use the entire email address or just a part of it as the search string. However, it is important that it is a unique string that does not overlap with other addresses.

New-InboxRule -Name ForwardPetr -Mailbox test-mailbox -RecipientAddressContainsWords "petr@" -ForwardTo bouska -StopProcessingRules $true 

Problem with Strings Longer Than 9 Characters

During testing, an unexpected problem emerged for which I found no information on the internet. If the string in the RecipientAddressContainsWords parameter was longer than nine characters, the rule was not applied (ignored).

Forwarding Rule - Filtering by Message Header

A functional solution turned out to be using a different condition, filtering by the message header. The condition HeaderContainsWords - with specific words in the message header. The rule applies to messages with a specific word in the email header.

New-InboxRule -Name ForwardPetr -Mailbox test-mailbox -HeaderContainsWords "<petr@" -ForwardTo bouska -StopProcessingRules $true 

Note: In my case, I had the addresses prepared in Excel. It was simple to generate individual commands directly there. The same could be done by loading from CSV.

Author:

Related articles:

Microsoft Exchange

Almost since the beginning of my practice, I have been involved in the administration of the Microsoft mail server, i.e. Exchange Server. I started with the 2003 version and worked my way up to Exchange Online. The articles cover many areas of management. Most since the migration to Exchange Server 2016 and its complete configuration. But also Exchange Hybrid and e-mail security.

If you want write something about this article use comments.

Comments

There are no comments yet.

Add comment

Insert tag: strong em link

Help:
  • maximum length of comment is 2000 characters
  • HTML tags are not allowed (they will be removed), you can use only the special tags listed above the input field
  • new line (ENTER) ends paragraph and start new one
  • when you respond to a comment, put the original comment number in squar brackets at the beginning of the paragraph (line)