Remove prefix from Email address RRS feed

  • General discussion

  • Hi 

    I have a script which removes the prefix left from the emailaddress of a user. But sometimes when the prefix comes in a different format such as LEFT.,Left,LEFT it does not remove them. Can someone guide me to modify the script which replaces these texts as well.

    $Username = "UserName"
    $Password = "Password" 
    $ServerName = "Servername"
    $securePassword = ConvertTo-SecureString $Password -AsPlainText -Force 
    $credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,$securePassword
    $PSSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://Servername/PowerShell -Authentication Kerberos -Credential $credential
    Import-PSSession $PSSession
    $email = Get-Mailbox 'Samaccountname' | select Alias,EmailAddresses
    $aliases = $email.EmailAddresses -replace "^(smtp:|SMTP:)left(?!@.*)",'$1' | sort -Unique
    Set-Mailbox 'Samaccountname' -EmailAddresses $aliases -HiddenFromAddressListsEnabled $false -ErrorAction Stop
    $ErrorMessage = $_.Exception.Message


    Friday, April 12, 2019 12:13 PM

All replies