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:


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


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")}


Schwarzenberg then becomes a.schwarzenberg:


Last but not least

Don’t forget to inform the users! 😉

6 replies »

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

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