Powershelll to Add to users Safe and Block

berket13 41 Reputation points
2020-08-18T17:50:35.977+00:00

Hello I am looking at the Powershell to add emails to a users black and white list:

Get-Mailbox | Set-MailboxJunkEmailConfiguration -BlockedSendersAndDomains @{Add="domain1.com", "@domain2.com"}-TrustedSendersAndDomains @{Add="domain3.com","@domain4.com"}

my question is can this be done for a bulk of uses imported from a csv file?

Thanks

Exchange Exchange Server Management
{count} votes

3 answers

Sort by: Most helpful
  1. Andy David - MVP 157.4K Reputation points MVP Volunteer Moderator
    2020-08-18T21:36:21.643+00:00

    Sure:

    Assuming you have a csv like this with a column called "BlockedSenders" called "blockedsenders.CSV"

    18542-image.png

    $mbx = Get-MailboxJunkEmailConfiguration <user>
    $list = Import-Csv .\blockedsenders.CSV
    $list | %{$mbx.BlockedSendersAndDomains +=$_.BlockedSenders}
    Set-MailboxJunkEmailConfiguration <user> -BlockedSendersAndDomains $mbx.BlockedSendersAndDomains

    Verify:

    Get-MailboxJunkEmailConfiguration <user>


  2. Lucas Liu-MSFT 6,191 Reputation points
    2020-08-19T05:28:42.8+00:00

    Hi @berket13 ,

    I agree with Andy.
    You could create a CSV file and add the users you want to batch import in the file. You could add the block users by running the commands above. If you want to add the trust users, you could substitute "Trusted" for "Blocked" in the commands.
    Please run the following command to add the trust user:

    $mbx = Get-MailboxJunkEmailConfiguration -Identity <>  
    $list = Import-Csv <>  
    $list | %{$mbx.TrustedSendersAndDomains +=$_.TrustedSenders}  
    Set-MailboxJunkEmailConfiguration -Identity <> -TrustedSendersAndDomains $mbx.TrustedSendersAndDomains  
    

    Below is my test add the trust users for user1 in test environment.
    18565-1111.png

    0 comments No comments

  3. Andy David - MVP 157.4K Reputation points MVP Volunteer Moderator
    2020-08-19T22:05:46.563+00:00

    Hi @berket13

    I assume you want to set a number of mailboxes to the same blocked senders:

    so lets create the loop within the loop

    First: The BlockedSenders.csv

    18836-image.png

    Then the list of mailboxes:
    MBXList.csv
    Column MBX
    18848-image.png

    Now the commands:

    $mbxlist = Import-Csv .\MBXList.csv
    $mbxlist |% {
    $mbx = Get-MailboxJunkEmailConfiguration $.MBX
    $list = Import-Csv .\blockedsenders.CSV
    $list | %{$mbx.BlockedSendersAndDomains +=$
    .BlockedSenders}
    Set-MailboxJunkEmailConfiguration $_.mbx -BlockedSendersAndDomains $mbx.BlockedSendersAndDomains
    }


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.