Changing EmailAddress value; is my code correct?

I want to change the domain address of Users, where exists but have some concerns.

  • I don’t want to remove the username from the whole EmailAddress, just change the domain part. Would the * work in this case?
Get-ADUser -Filter * -Properties EmailAddress | select samAccountName,EmailAddress |
    Where-Object {($_.EmailAddress -Like "*@oldDomain.com")} |
        Set-ADUser -Replace @{EmailAddress = "*@newDoamin.com"}

Am I close to getting this correct?

wildcard can be used to filter but not for replace. You can try

Set-ADUser -Replace @{$_.EmailAddress = ($_.name + '@newDoamin.com')}

Thank you,

if I then adjust slightly for an input file thus,

Import-Csv .\Users.csv | % {
Get-ADUser -Identity $_.Users -Properties EmailAddress | select samAccountName,EmailAddress |
    Where-Object {($_.EmailAddress -Like "*@oldDomain.com")} |
       Set-ADUser -Replace {$_.EmailAddress = ($_.name + '@NewDomain.com')}
}

…does this look correct?

I get the following error:

Set-ADUser : Cannot bind parameter 'Replace'. Cannot convert the "$_.EmailAddress = ($_.name + '@NewDomain.com')" value of type 
"System.Management.Automation.ScriptBlock" to type "System.Collections.Hashtable".

You can’t use -Replace with Set-ADUser that way. It needs to be a hash table as per the docs / help file on the topic.

# get function / cmdlet details
(Get-Command -Name Set-ADUser).Parameters
Get-help -Name Set-ADUser -Examples
Get-help -Name Set-ADUser -Full | Out-Notepad
Get-help -Name Set-ADUser -Online

Example 3: Set properties

Set-ADUser -Identity GlenJohn -Replace @{title="director";mail="glenjohn@fabrikam.com"}

What is the structure of your CSV file?

I have this:

[Users.csv ]

User,
samAccountName01
samAccountName02
samAccountName03

and this new code since I now have captured the OldDomain values.

Import-Csv .\Users.csv | % {
Get-ADUser -Identity $_.User -Properties EmailAddress | select samAccountName,EmailAddress |
       Set-ADUser -Replace {$_.EmailAddress = ($_.name + '@NewDomain.com')}
}

…but postanote suggests I need to revise it still for Set-ADuser

remember look at the example.
You are not using the ‘@’ symbol in your Set-ADUser code.

Set-ADUser -Replace @{$_.EmailAddress = ($_.name + '@NewDomain.com')}

postanote I revised thus:

Import-Csv .\users.csv | % {
Get-ADUser -Identity $_.User -Properties EmailAddress | 
       Set-ADUser -Replace @{$_.EmailAddress = ($_.Name + '@NewDomain.com')}
}

My .csv is structured like:

[users.csv]
User
OneTestUserOnly

…but I get error “A null key is not allowed in a hash literal.”

I think it’s choking on $_.Name (I even tried samAccountName) but don’t know why other than it’s not in the pipeline and not sure how to make it available. We wouldn’t assume that the Name or samAccountName or the displayName would be the same as the emailaddress before the @sign. I simply want to change the @oldDomain to @NewDomain.

My colleague got it work with this:

$Users = Get-Content -Path C:\Scripts\Users.csv 
foreach ($user in $Users) {
    $aduser = Get-ADUser $user -Properties EmailAddress
    If($aduser.EmailAddress -like "*@oldDomain.com")
        {$newemail = $aduser.samaccountname + '@NewDomain.com'
        Set-ADUser -Identity $user -EmailAddress $newemail
        }
}

Assuming that the email isn’t solely based on the SamAccountName @ domain.com you can split the email address and reuse the ‘prefix’:

$Users = Get-Content -Path C:\Scripts\Users.csv 
foreach ($User in $Users) {
    $ADUser = Get-ADUser $User -Properties EmailAddress
    If($ADUser.EmailAddress -match '@oldDomain.com$') { # Has to end on the @oldDomain.com
        $EmailPrefix = ($ADUser.EmailAddress -split '@')[0] # Gets the part before @
        $NewEmail = $EmailPrefix + '@NewDomain.com'
        Set-ADUser -Identity $User -EmailAddress $NewEmail
    }
}

If you by email, mean the email the user is using to log in, then it’s actually not the EmailAddress you have to change alone, since it’s only the one being displayed, but the UserPrincipalName, which also can be set by adding the -UserPrincipalName parameter to the Set-ADUser command like:

But be aware of this, since it will affect the user in a whole other way, and the “newDomain.com” should be select-able when creating new users.

$Users = Get-Content -Path C:\Scripts\Users.csv 
foreach ($User in $Users) {
    $ADUser = Get-ADUser $User -Properties EmailAddress
    If($ADUser.EmailAddress -match '@oldDomain.com$') { # Has to end on the @oldDomain.com
        $EmailPrefix = ($ADUser.EmailAddress -split '@')[0] # Gets the part before @
        $NewEmail = $EmailPrefix + '@NewDomain.com'
        Set-ADUser -Identity $User -EmailAddress $NewEmail -UserPrincipalName $NewEmail
    }
}

Your first scenario would be more correct for our environment, thanks Tom.