Forum Discussion

Robert Bollinger's avatar
Robert Bollinger
Brass Contributor
Sep 13, 2021

Mailbox Rules, PowerShell and Scripting.

Hey Guys, 

 

I am trying to create a powershell script that will create a mailbox rule in each users mailbox, based on an input list. 

 

Here is what i have so far: 

 

New-InboxRule -Mailbox $SingleImport.PrimarySMTPAddress -From user.name@extdomain.com -MyNameInToBox $True -SubjectContainsWords '["Receipt Copy"] Hello' -Name EmailRule -MoveToFolder $SingleImport.PrimarySMTPAddress:\"Sent Items"

 

My input list has a single Column:

 

PrimarySMTPAddress

user.name@ourfqdn.com

user.name2@ourfqdn.com

user.name3@ourfqdn.com

 

I am getting this error when trying to run the script:

 

A positional parameter cannot be found that accepts argument ':\Sent Items'.
+ CategoryInfo : InvalidArgument: (:) [New-InboxRule], ParameterBindingException
+ FullyQualifiedErrorId : PositionalParameterNotFound,New-InboxRule
+ PSComputerName : outlook.office365.com

 

Any ideas? 

 

Thanks, 

Robert 

  • pvanberlo's avatar
    pvanberlo
    Steel Contributor
    Try this:

    New-InboxRule -Mailbox $SingleImport.PrimarySMTPAddress -From user.name@extdomain.com -MyNameInToBox $True -SubjectContainsWords '["Receipt Copy"] Hello' -Name EmailRule -MoveToFolder “$($SingleImport.PrimarySMTPAddress):\Sent Items"
    • Robert Bollinger's avatar
      Robert Bollinger
      Brass Contributor

      pvanberlo 

      So that seemed to work!! Thanks for your help. However now i have a different problem, instead of operating on each object (each user in my list) it only applies the mailbox rule to the LAST user in the list. 

       

      I have not seen that before? Any ideas? 

       

      Thanks

      • pvanberlo's avatar
        pvanberlo
        Steel Contributor
        Would need a bit more of your script. But generally you’d do something like this:

        $Mailboxes = Import-CSV .\mailboxes.csv
        ForEach ($Mailbox in $Mailboxes) {
        // Insert the code above but replace SingleImport with Mailbox
        }

        On mobile right now, so can’t make it pretty etc.

Resources