Need help to rename all users in Specific OU to lowercase and then some RRS feed

  • Question

  • Hello All,

    I would like to rename all my users in a specific OU to all lower Case, except their Surname I would like that to be uppercase.  Then change their name(CN) based on givenname (lowercase) + surname (uppercase)

    This is the line that is buggy ($newname = "$user.givenname+$user.surname")....this line gets my result but adds no space between GN&SN

    FredJones.........I need Fred Jones with no space

    I have the following script and would like to know how I can get the above result:

    # Get all the users in certain OU
    foreach ($user in (Get-ADUser -SearchBase "OU=Test,OU=Quest,DC=CCSMTL,DC=rtss,DC=qc,DC=ca" -filter * -property displayname, givenname, surname, cn, samaccountname, userprincipalname, Name, description, office)) {
    # Change displayname
    $newdisplay = $user.displayname.Tolower()
    # Change givenname
    $newgiven = $user.givenname.Tolower()
    # Change surname
    $newsurn = $user.surname.ToUpper()
    # Change SAM
    $newsam = $user.samaccountname.Tolower()
    # Change UPN
    $newupn = $user.userprincipalname.Tolower()
    # Change name
    $newname = "$user.givenname+$user.surname"
    # Change Description
    $newdesc = $user.description.Tolower()
    # Change Office
    $newoffice = $user.office.Tolower()

    #Set new values
    Set-ADUser -identity "$user" -description $newdesc
    Set-ADUser -identity "$user" -office $newoffice
    Set-ADUser -identity "$user" -displayname $newdisplay
    Set-ADUser -identity "$user" -givenname $newgiven
    Set-ADUser -identity "$user" -surname $newsurn
    Set-ADUser -identity "$user" -samaccountname $newsam
    Set-ADUser -identity "$user" -userprincipalname $newupn
    Rename-adobject -identity "$user" -newname $newname


    • Moved by Bill_Stewart Tuesday, July 31, 2018 3:02 PM This is not "debug/fix/rewrite my script for me" forum
    Friday, June 22, 2018 4:32 PM

All replies

  • A complication is that AD is case aware, but not case sensitive. This means you cannot modify an attribute value if the only change is to the case. If the current value is "Jim Smith", you cannot change it to "jim smith". You need to first change it to a different value, wait for that to replicate, then change it to what you want. At least that is my recollection.

    Richard Mueller - MVP Enterprise Mobility (Identity and Access)

    Friday, June 22, 2018 4:41 PM
  • Just add a space between the two names.

    You can set all values in one command.

    Set-ADUser -identity $user -description $user.description.Tolower() -office $user.office.Tolower() … etc

    If you do all of those round trips the script will be very slow.


    Friday, June 22, 2018 4:44 PM