PowerShell - Add Junk email Configuration for multiple users


PowerShell - Add Junk email Configuration for multiple users


Summary

This TechNet Wiki is based on the Office365 Forum Post. Refer this link for more information.

Requirement

Add Junk email Configuration for multiple users "each have different trusted senders" (csv file).

Solution

This is very straight forward approach because the input CSV structure is not complex. The format what OP had is shown below

Code Used by OP
import-csv .\sample.csv | 
foreach {Set-MailboxJunkEmailConfiguration -identity $_.user -TrustedSendersAndDomains $_.List}
Error

The Column domain is multivalued and it's separated by comma.

Using Regex Split

We can suggest multiple solutions. In this TechNet Wiki let's demo REGEX Split menthod

Import-Csv C:\domains.csv | %{
Set-MailboxJunkEmailConfiguration -Identity $_.User `
                                  -TrustedSendersAndDomains @{add=[regex]::Split($_.Domain , ",")} -Verbose
}
Explanation


We simply used [REGEX]::Split method and picked the domain names as individual object and adding those in the user's mail junk mail trusted configuration.
About TrustedSendersAndDomains -TrustedSendersAndDomains

  • The TrustedSendersAndDomains parameter specifies a list of the individual senders and domains that are considered trusted senders. Messages from these senders
  • and domains aren't processed by the junk email rule.
  • To enter multiple values and overwrite any existing entries, use the following syntax: ,.... If the values contain spaces or otherwise require quotation marks, you need to use the following syntax: "",""....
  • To add or remove one or more values without affecting any existing entries, use the following syntax: @{Add="",""...;
  • Remove="",""...}.

So we can add multiple domains like @{add="domain1.com" , "domain2.com"} but we can't do @{add=@domain.com} because it's an invalid domain entry.
Similarly to remove it we should use @{remove="domain.com"}