select users with specific SMTP address domain & change display name with a format

Stefanos Constantinou 61 Reputation points
2022-02-24T16:10:37.48+00:00

Hello all,

I need to select users in a specific OU with a specific domain and change their display name.

I also need to run this script and get the change only for the users which do not have the specific Display Name format

my current script is below:

$Users = Get-ADUser -SearchBase "ou=Test OU,dc=domain,dc=com" -Filter {(PrimaryProxyAddress -Like "*@keyman .com")} -Properties DisplayName | Select DisplayName, GivenName, Surname, Name, DistinguishedName
ForEach ($User In $Users)
{
$DN = $User.DistinguishedName
$First = $User.GivenName
$Last = $User.Surname
$CN = $User.Name
$Display = $User.DisplayName
$NewName = "$First $Last | Domain Services"
If ($Display -ne $NewName) {Set-ADUser -Identity $DN -DisplayName $NewName}
If ($CN -ne $NewName) {Rename-ADObject -Identity $DN -NewName $NewName}}

pause

somehow it does not work for me and I get no errors
:-/

Windows for business Windows Client for IT Pros Directory services Active Directory
Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-02-25T17:46:19.613+00:00

    Hi @Stefanos Constantinou ,

    please try this and take a look if the PrimaryProxyAddress property contains the value you expected:

    Get-ADUser -SearchBase "ou=Test OU,dc=domain,dc=com" -Properties *  
    

    You should check if the name PrimaryProxyAddress is correct. Could it be the property name is PrimaryProxyAddresses ?

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


2 additional answers

Sort by: Most helpful
  1. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-02-24T17:29:07.61+00:00

    Hi @Stefanos Constantinou ,

    maybe this works better (not tested by myself):

    $Users = Get-ADUser -SearchBase "ou=Test OU,dc=domain,dc=com" -Filter { (PrimaryProxyAddress -Like "*@domain.com") } -Properties *  
    ForEach ($User In $Users) {  
      $DN = $User.DistinguishedName  
      $First = $User.GivenName  
      $Last = $User.Surname  
      $CN = $User.Name  
      $Display = $User.DisplayName  
      $NewName = "$First $Last | Domain Services"  
      If ($Display -ne $NewName) { Set-ADUser -Identity $DN -DisplayName $NewName }  
      If ($CN -ne $NewName) { Rename-ADObject -Identity $DN -NewName $NewName }  
    }  
    Pause  
    

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Andreas Baumgarten 123.4K Reputation points MVP Volunteer Moderator
    2022-02-25T08:56:00.207+00:00

    Hi @Stefanos Constantinou ,

    any error message?
    Maybe you should add some "outputs" in the script to see what happens. For instance the output of $user, $Display, $CN to see the values and if these fitting the if clauses.

    ----------

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.