Author Posts

April 25, 2015 at 3:56 am

Hi there

I am trying to create a script that will check whatever is the primary email for a user

lets say david23@test.corp copy whats before the @ sign and assign a new policy

for example david23@testing.com.

the long story:

I have a lot of users that aren't using the default email policy so they have custom email address that I have no clue what they are(thousands of users like that) so if I modify the default email policy that's based on firstname.lastname@test.corp and just add firstname.lastname@testing.com it wont apply for them, so I have to find away to add the relevant email for these.

more of powershell thing then exchange.

Thanks a lot for any advice

April 25, 2015 at 4:47 am

Please search for 'PowerShell regular expressions email address" using your favourite search engine. Alternatively use the -split operator or Split method on a string variable containing the email address.

https://technet.microsoft.com/en-us/library/hh847811.aspx

April 25, 2015 at 5:09 am

Thanks
im on it
im thinking of something like this:

$string=Get-Mailbox |Select-Object PrimarySmtpAddress
$split=$string.primarysmtpaddress.Split("{@}")
$before=$split.GetValue(0)
$before
set-mailbox -ErrorAction:Stop -Identity $user.identity -EmailAddresses @{Add=($before + "@" + "new.mail.onmicrosoft.com")}
$newemail=Get-Mailbox |Select-Object -expand -property emailaddresses

April 25, 2015 at 5:50 am

I do not have access to Exchange but you should be able to do something like below:

$NewDomain = 'new.mail.onmicrosoft.com'
foreach ($Mailbox in Get-Mailbox) {

    if ($Mailbox.PrimarySmtpAddress -notlike "@$NewDomain") {
        
        $LocalPart = $Mailbox.PrimarySmtpAddress.Split('@')[0]

        $Mailbox | Set-Mailbox -EmailAddresses @{
            Add = ($LocalPart + "@$NewDomain")
        }

        ($Mailbox | Get-Mailbox).EmailAddresses
    }
}

April 25, 2015 at 6:17 am

awesome
thanks

April 25, 2015 at 6:30 am

I have a small stupid question(for my learning)
how would I know that "add" is an option on the emailaddresses property?
Thanks again

April 25, 2015 at 6:43 am

The help or online documentation of the cmdlet should tell you how to use the parameters. Unfortunately the Exchange cmdlet documentation is lacking in that area. You will need to use your favorite search engine to find out what is going on, unfortunately.

April 25, 2015 at 7:11 am

yeah I was just wondering,
Thanks