A PowerShell script to add a Microsoft Outlook rule that shows a warning when you get E-mail with an attachment 2


Okay so a visitor to Microsoft PowerShell forums asked how to set Microsoft Outlook to cast a message with a warning if a mail contained an attachment. I wrote a quick hack for him and I thought I would share it with anyone who is reading this blog or (more likely) stumbled in here from Google. Here you go.

$outlook = New-Object -ComObject outlook.application
$rules = $outlook.Session.DefaultStore.GetRules()
$olRuleType = "Microsoft.Office.Interop.Outlook.OlRuleType" -as [type]
$rule = $rules.Create("MyNewRule",$olRuleType::OlRuleReceive)
$FromCondition = $rule.Conditions.HasAttachment
$FromCondition.Enabled = $true
$RuleAction = $rule.Actions.NewItemAlert
$RuleAction.Text = "This is a demo script"
$RuleAction.Enabled = $true


The script generates a warning that looks like this

Please leave a comment!

Best regards
Karl-Henrik Nilsson.

  • sihai

    thank you for your example.

    I have a question for the rule.

    can I the attachment in the mail as a file copy ?

    Best regards


    • http://karl-henrik.se/ Karl-Henrik Nilsson

      Not sure I follow. What would you like the script to do when it sends the alert?