locked
Listing all accounts with certain alias type and only outputting those emailaddresses RRS feed

  • Question

  • Hi there,

    I'm tasked with writing a script to output all emailaddresses with a certain domain ie

    standard address is @company.co.uk

    address to find is @company.xyz.co.uk

    The point of the script will be to find all of these addresses and pipe them into a command to remove those addresses from the mailbox, but obviously for that I need the result to only show the specified addresses and not all the addresses as my command does so far.

    The script so far is:

    Get-Mailbox -resultsize unlimited -Filter {emailaddresses -like "*@company.xyz.co.uk"} | select -expand emailaddresses | %{$_.smtpaddress}

    Any help would be great :)



    • Edited by 3d7omb Wednesday, March 25, 2015 11:41 AM
    Wednesday, March 25, 2015 11:31 AM

Answers

  • Actually, it's simpler if you keep all the addresses.  Try something like the following:

    Get-Mailbox -resultsize unlimited -Filter { EmailAddresses -like "*@company.xyz.co.uk" } | % {
        $Addresses = $_.EmailAddresses | ? { $_.SmtpAddress -notlike "*@company.xyz.co.uk" }
        Set-Mailbox $_ -EmailAddresses $Addresses
    }

    I'm using your same filter so only those mailboxes with your bad email addresses are returned in the search.  Then I select only the addresses you wish to retain.  Finally, I re-add all the good addresses to the mailbox.

    There are other ways to do this, but I've found that none works all the time with any multi-valued field as well as this one does.  If you really want those other ways, I can do a bit of research to find them.

    • Marked as answer by 3d7omb Wednesday, March 25, 2015 4:06 PM
    Wednesday, March 25, 2015 1:16 PM

All replies

  • Actually, it's simpler if you keep all the addresses.  Try something like the following:

    Get-Mailbox -resultsize unlimited -Filter { EmailAddresses -like "*@company.xyz.co.uk" } | % {
        $Addresses = $_.EmailAddresses | ? { $_.SmtpAddress -notlike "*@company.xyz.co.uk" }
        Set-Mailbox $_ -EmailAddresses $Addresses
    }

    I'm using your same filter so only those mailboxes with your bad email addresses are returned in the search.  Then I select only the addresses you wish to retain.  Finally, I re-add all the good addresses to the mailbox.

    There are other ways to do this, but I've found that none works all the time with any multi-valued field as well as this one does.  If you really want those other ways, I can do a bit of research to find them.

    • Marked as answer by 3d7omb Wednesday, March 25, 2015 4:06 PM
    Wednesday, March 25, 2015 1:16 PM
  • Modified your code a little bit and it works a treat. Thank you!!!

    The code I used was

    Import-Csv C:\temp\test.csv | foreach-object{
    	$user = $_.user 
    	$Addresses = Get-Mailbox $user | select -ExpandProperty emailaddresses | where {$_.smtpAddress -notlike "*@company.xyz.co.uk"}
    	 
    	set-Mailbox $user -EmailAddresses $Addresses
    }
    

    Wednesday, March 25, 2015 4:02 PM
  • Glad to help out.  (-:
    Wednesday, March 25, 2015 5:11 PM