Outlook rules automatic generation thanks to Powershell

Dear Community,

I am currently trying to generate an outlook rule for incoming mail automatically with data coming from some previous Power Automate variables (Number & Outlook Folder).

The rule is supposed to look for a specific number (variable) in any incoming mail subject and move it in a specific folder.

In a first step I skipped the variable aspect to focus on the script itself and unfortunately even at this stage I have some troubles.

Below the script itself:

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Inbox = $Namespace.GetDefaultFolder(6)
$SubFolder = $Inbox.Folders.Item("#Segment")

#Setting the name of the rule
$Rules = $Namespace.DefaultStore.GetRules()
$Rule = $Outlook.Session.DefaultStore.GetRules().Create("Rule Name", [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive)

#Setting the condition: looking for the word "dedicated" in the subject of the mail
$Condition = $Rule.Conditions.Subject
$Condition.Enabled = $true
$Condition.Text = @("Dedicated")

#Action of the rule
$Action = $Rule.Actions.MoveToFolder
$Action.Enabled = $true
$Action.Folder = $Subfolder

#Enabling of the rule and saving showing a progress popup
$Rule.Enabled = $true
$Rules.Save($true)

This script generates the error below:

One or more rules cannot be saved because of invalid actions or conditions.
At line:1 char:1
+ $Rule.Enabled = $true
+ ~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : OperationStopped: (:) [], COMException
    + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

Obviously something goes wrong but after hours of web research I could’t fix the solution.
Any suggestion would be welcome.

Hi, welcome to the forum :wave:

If you output $Action you’ll see that the folder property is NULL. So there’s your problem:

PS C:\Users\Matt B> $Action

Application : Microsoft.Office.Interop.Outlook.ApplicationClass
Class       : 118
Session     : Microsoft.Office.Interop.Outlook.NameSpaceClass
Parent      : System.__ComObject
Enabled     : True
ActionType  : 1
Folder      :

I tried various ways to get it to take a value, but nothing worked until I found this answer on StackOverflow:

Adapting it to your code:

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Inbox = $Namespace.GetDefaultFolder(6)
$SubFolder = $Inbox.Folders.Item("#Segment")

#Setting the name of the rule
$Rules = $Namespace.DefaultStore.GetRules()
$Rule = $Outlook.Session.DefaultStore.GetRules().Create("Rule Name", [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive)

#Setting the condition: looking for the word "dedicated" in the subject of the mail
$Condition = $Rule.Conditions.Subject
$Condition.Enabled = $true
$Condition.Text = @("Dedicated")

#Action of the rule
$Action = $Rule.Actions.MoveToFolder
$Action.Enabled = $true
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember(
    "Folder",
    [System.Reflection.BindingFlags]::SetProperty,
    $null,
    $Action,
    $SubFolder
)

#Enabling of the rule and saving showing a progress popup
$Rule.Enabled = $true
$Rules.Save($true)
2 Likes

Dear Matt,

Many thanks for the solution you send and the time spend on my post.

Even if it seems to work well - the script runs without error - the rule was not created in Outlook.
I updated the script thanks to the help of an IA and it works well now.

Below the final script.

$Outlook = New-Object -ComObject Outlook.Application
$Namespace = $Outlook.GetNameSpace("MAPI")
$Inbox = $Namespace.GetDefaultFolder(6)
$SubFolder = $Inbox.Folders.Item("#Segment")

#Setting the name of the rule
$Rules = $Namespace.DefaultStore.GetRules()
$Rule = $Rules.Create("Rule Name 2", [Microsoft.Office.Interop.Outlook.OlRuleType]::olRuleReceive)

#Setting the condition: looking for the word "dedicated" in the subject of the mail
$Condition = $Rule.Conditions.Subject
$Condition.Enabled = $true
$Condition.Text = @("Dedicated")

#Action of the rule
$Action = $Rule.Actions.MoveToFolder
$Action.Enabled = $true
[Microsoft.Office.Interop.Outlook._MoveOrCopyRuleAction].InvokeMember(
    "Folder",
    [System.Reflection.BindingFlags]::SetProperty,
    $null,
    $Action,
    $SubFolder
)

#Enabling of the rule and saving showing a progress popup
$Rule.Enabled = $true
$Rules.Save($true)

#Display of the number of rules
$numberofrules = $outlook.Session.DefaultStore.GetRules();
[Console]::WriteLine([string]::Format("There are {0} rules", $numberofrules.Count));