Exchange delete emailaddress for users from csv file

Hello,

I wolud like to remove for all users in csv, emailaddress on specific domain. I used something like this, but not working. Could Someone help me to solve this error?

foreach ($q in $importcsv) | ? {$_.AddressString -like "*@domain.pl"} | %{ {Set-Mailbox $q.login -EmailAddresses @{remove="$_"}}}

Error below:

At C:\Users\lcholewa\Desktop\migracja_csv.ps1:54 char:27
+ foreach ($q in $importcsv) | ? {$_.AddressString -like "*@domain.pl"} | %{ {Set ...
+                           ~
Missing statement body in foreach loop.
At C:\Users\lcholewa\Desktop\migracja_csv.ps1:54 char:28
+ foreach ($q in $importcsv) | ? {$_.AddressString -like "*@domain.pl"} | %{ {Set ...
+                            ~
An empty pipe element is not allowed.
    + CategoryInfo          : ParserError: (:) [], ParseException
    + FullyQualifiedErrorId : MissingForeachStatement

BR/Lukas

March 31st, 2015 11:40am

Hi,

To delete emailaddress for all users, I recommend this script

https://gallery.technet.microsoft.com/ScriptCenter/0d773a0b-0eaa-4679-bb17-d6b8caa79d2e/

Save them in a ps1-file and dot source them, then you can use this function in EMS.

After that, use the following command to remove emailaddress for all users from CSV file.

$Export = Get-Content C:\Users\AdministratorAccount\Desktop\Mailbox.csv

foreach ($mb in $Export) { $mb | Remove-EmailAddress -EmailDomain domain.com }

Note: Replace the path in my command and specify the domain.com you want to remove after parameter EmailDomain.

Mailboxes in my CSV look like this:

Additionally, if the mailboxes from CSV file contains all users in your organization, we can easily use this command to remove email address.

Get-Mailbox | Foreach-Object {Remove-EmailAddress -EmailDomain nourd.com -Mailbox $_}

If there are any other problem, please let me know.

Best Re

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 3:04am

Hello,

Thank You for answer. In Exchange 2013 i don't have cmdlet "remove-emailaddress" i have only "Remove-EmailAddressPolicy".

Below link does not have this cmdlet to.

https://technet.microsoft.com/en-us/library/jj218649%28v=exchg.150%29.aspx

I build script in two versions:

One based on, input only one user login at a time and:

set primary smtp

delete emailaddress for specific domain. I used here this command "Set-Mailbox $login -EmailAddresses @{remove="$login$emailadres"}"

create mail-contact

set forward to created contact

export to pst file.

This version of script working in 100%. :D. One login at a time is not a problem.

But second version of script based on imported CSV file. And I stuck on remove emailaddress :(. And i want to implement all steps in one script.

BR/Lukas

April 1st, 2015 4:42am

Hi,

1. That script add two functions (Add-EmailAddress and Remove-EmailAddress) to Exchange. Then you can use Remove-EmailAddress cmdlet that is not available in Exchange originally. I test that script in my lab, see the screenshot below:

2. As I said, if you want remove email address for all users in your organization, there is no need to export them to PST (Unless you need to report)

Best Re

Free Windows Admin Tool Kit Click here and download it now
April 1st, 2015 5:54am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics