Copilot for Microsoft 365 Tech Accelerator
Feb 28 2024 07:00 AM - Feb 29 2024 10:30 AM (PST)
Microsoft Tech Community

Mailbox Rules, PowerShell and Scripting.

Brass Contributor

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 

7 Replies
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"

@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

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.

@pvanberlo 

 

I forgot to post this last night: 

 

$Importlist = Import-CSV Test_Email_Rule.csv

foreach ($SingleImport in $importlist)
{
New-InboxRule -Mailbox $SingleImport.PrimarySMTPAddress -From user.name@domain.com -MyNameInToBox $True -SubjectContainsWords '["Receipt Copy"] Hello' -Name TestEmailRule -MoveToFolder “$($SingleImport.PrimarySMTPAddress):\Sent Items"
}

 

Thats my complete script. 

@pvanberlo 

ok so here is where i am on it. So far this almost works: 

 

$Importlist = Import-CSV Test_Email_Rule.csv

foreach ($SingleImport in $importlist)
{
New-InboxRule -Mailbox $($SingleImport.PrimarySMTPAddress) -From user.name@domain.com -MyNameInToBox $True -SubjectContainsWords '["Receipt Copy"] Hello' -Name RAKEmailG4 -MoveToFolder "$($SingleImport.PrimarySMTPAddress):\Sent Items"
}

 

The above script appears to work, however when it comes to moving the item to the users sent items folder it fails. 

 

If i remove MovetoFolder Option and select -Deletemessage $True then it works. However i dont want to delete the message. 

 

I am attaching an screenshot as well. 

Showing errorShowing error

Hmm. The error message seems to indicate a connection failure. Not sure what might be causing it, but the syntax should be correct reading the docs.

@pvanberlo 

 

Thanks for the response. Actually it turns out the problem was related to not having full mailbox access rights to the mailboxes in which i am trying to create the rule. I was testing with my 3 test accounts and those worked as expected. However my other test accounts did not. 

 

I did some additional checking and found an article covering the need for full mailbox rights, but only when setting a rule which touches the users mailbox folders. 

 

Thanks for your help.