Changing EmailAddress value; is my code correct?

Welcome Forums General PowerShell Q&A Changing EmailAddress value; is my code correct?

This topic contains 11 replies, has 5 voices, and was last updated by

 
Participant
1 week ago.

  • Author
    Posts
  • #113740

    Participant
    Points: 0
    Rank: Member

    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?

  • #113747

    Participant
    Points: 65
    Published
    Rank: Member

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

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

      Participant
      Points: 0
      Rank: Member

      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".
  • #113759

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

    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"}
    
  • #113770

    Participant
    Points: 0
    Rank: Member

    What is the structure of your CSV file?

    • #113771

      Participant
      Points: 0
      Rank: Member

      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

  • #113794

    Participant
    Points: 37
    PublishedHelping Hand
    Rank: Member

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

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

      Participant
      Points: 0
      Rank: Member

      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.

  • #113891

    Participant
    Points: 0
    Rank: Member

    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
            }
    }
  • #113984

    Participant
    Points: 0
    Rank: Member

    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
        }
    }
    
    • #113996

      Participant
      Points: 0
      Rank: Member

      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
          }
      }
      
    • #114147

      Participant
      Points: 0
      Rank: Member

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

You must be logged in to reply to this topic.