PowerShell

PowerShell: Changing Active Directory user logon names (Bulk)

Active Directory users log on with their logon names and password. But what are the rules for assigning usernames? g.surname? surname? gsurname? What are the naming conventions? This article looks for and modifies users who do not meet the naming convention.

Searching for logon names that do not match the naming convention

For the following it is assumed that you use a naming convention of g.surname for all newly created users (Patrick Gruenauer = p.gruenauer). So, we have to search for usernames that don’t have a dot (.). Here’s one:

4.JPG

Now we are looking for users in a particular Organizational Unit (People) that do not meet the naming convention.


Get-ADUser -Filter {SamAccountName -NotLike "*.*"} -Searchbase "OU=People,DC=SID-500,DC=COM" | Select-Object Name,SamAccountName,Userprincipalname

Unbenannt.JPG

Check the list carefully. In the next step we will start modifying their SamAccountName and Userprincipalname.

Changing user logon names

Now we are going to replace the SamAccountName and the UserprincipalName with the first letter of the givenname followed by . and the lastname in lower case.


Get-ADUser -Filter {(SamAccountName -NotLike "*.*") -and (UserprincipalName -notlike "*.*@*")} -SearchBase "OU=People,DC=SID-500,DC=COM" | Foreach-Object {Set-ADUser $_ -SamAccountName ($_.givenname.substring(0,1) + '.' + $_.surname).tolower() -UserPrincipalName (($_.givenname.substring(0,1) + '.' + $_.surname).tolower() + "@" + "$env:userdnsdomain")}

3.JPG

Schwarzenberg then becomes a.schwarzenberg:

3.JPG

Last but not least

Don’t forget to inform the users! 😉

15 replies »

  1. I?m impressed, I have to admit. Rarely do
    I come across a blog that?s equally educative and entertaining, and let
    me tell you, you have hit the nail on the head. The issue is something not enough folks are speaking intelligently about.
    Now i’m very happy that I came across this during my hunt for something regarding this.

    Like

  2. Hello Patrick,
    Thank you for the post.
    Just want to check what would be string to have the username in full? I.E. for John Doe = John.Doe. Will the below work?

    $firstname. + ‘.’ + $lastname

    Many Thanks,
    Pavan

    Like

  3. Hello Patrick. Thank you for the post. Just want to check what will be string if i need the username as First.Lastname in Full. For example for John Doe = John.Doe.
    Will the below work?
    $firstname. + ‘.’ + $lastname

    Many Thanks,
    Pavan

    Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.