web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Automate / Outlook rules automati...
Power Automate
Answered

Outlook rules automatic generation thanks to Powershell scripting vs power automate recorder

(0) ShareShare
ReportReport
Posted on by 41

Dear Community,

 

I am currently trying to generate an outlook rule for incoming mail automatically by filling some of the rules fields 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.

I reached some results using the recorder but it seems that this solution is not really robust so I tried to go on some Powershell scripting.

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

 

$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 error is generated and despite some hours of research I could not find the solution to fix it.

 

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

 

Any suggestion would be helpfull.

 

Many thanks in advance.

I have the same question (0)
  • Agnius Bartninkas Profile Picture
    Most Valuable Professional on at

    Not that I've ever done it myself, but a Google search suggested using the New-InboxRule cmdletfor doing this, if you are applying it to an Exchange Online email. It will apply the rule to the inbox, and not the outlook instance, which means the rule will work even when Outlook is not running. See here on how to use it: https://learn.microsoft.com/en-us/powershell/module/exchange/new-inboxrule?view=exchange-ps

     

    Other than that, have you tried creating the rule without the $Rule.Enabled = $true part?

    -------------------------------------------------------------------------
    If I have answered your question, please mark it as the preferred solution. If you like my response, please give it a Thumbs Up.

    I also provide paid consultancy and development services using Power Automate. If you're interested, DM me and we can discuss it.

  • Verified answer
    Emmanuel9 Profile Picture
    41 on at

    Dear Agnius, Many thanks for your answer. Unfortunately the solution you propose require some exchange privilege simple users can't get. In the meantime I found the solution and past it below.

    $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));

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Automate

#1
Vish WR Profile Picture

Vish WR 862

#2
Valantis Profile Picture

Valantis 738

#3
Haque Profile Picture

Haque 553

Last 30 days Overall leaderboard