Link to home
Start Free TrialLog in
Avatar of rsnellman
rsnellmanFlag for United States of America

asked on

Need assistance in changing my current usernames in AD to lowercase

Hi, it has been awhile since I needed the expert's advice/assistance.  Not sure what that means.  LOL.

Anyways, I am needing to change all usernames (full names) of a particular OU to all lowercase for a database migration.  I thought I read somewhere a long time ago about such a thing or technique to accomplish this.

Currently, I am running...

Server: Windows Server 2003 - Standard
Active Directory : LDAP
OU: contains about 3500+ accounts

The usernames first letter are capitalized and the rest lowercase.  I am required to modify all these usernames to all lowercase.

Any ideas or links would be amazingly appreciated.

Thanks for your time.

Bob
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, I just did the same thing, but for the samAccountName of every user in my AD.  Here is the script that did that, as well as writing a log of what changed.

If you need assistance changing it to modify only First Name, Last Name, and Display Names, I can help with that.

You can restrict it to a certain OU, by changing
FROM 'LDAP://" & strDNSDomain & "' WHERE

to
FROM 'LDAP://OU=Users,OU=OfficeA," & strDNSDomain & "' WHERE

Regards,

Rob.
Option Explicit
Dim objCommand, objConnection, objRecordset
Dim objRootDSE, strDNSDomain, strNTName, strNewName, strNewPN, strUserDN, strUserPN, objUser
Dim boolNeedToChange
Dim objFSO, strLogFile, objLogFile

' Search entire Active Directory domain.
Set objRootDSE = GetObject("LDAP://RootDSE")
strDNSDomain = objRootDSE.Get("defaultNamingContext")

Const ADS_SCOPE_SUBTREE = 2

Set objConnection = CreateObject("ADODB.Connection")
Set objCommand =   CreateObject("ADODB.Command")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"
Set objCommand.ActiveConnection = objConnection

objCommand.Properties("Page Size") = 1000
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE 

objCommand.CommandText = _
    "SELECT sAMAccountName,distinguishedName,userPrincipalName FROM 'LDAP://" & strDNSDomain & "' WHERE objectCategory='person' AND objectClass='user'"

' Run the query.
Set objRecordset = objCommand.Execute

strLogFile = "Convert_All_User_Login_IDs_To_LowerCase_Log.txt"
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objLogFile = objFSO.CreateTextFile(strLogFile, True)
objLogFile.WriteLine "Script started: " & Now

' Enumerate the resulting recordset.
While Not objRecordSet.EOF
	' Retrieve values and display.
	strNTName = objRecordset.Fields("sAMAccountName").Value
	strUserDN = objRecordset.Fields("distinguishedName").value
	strUserPN = objRecordset.Fields("userPrincipalName").value
	' Make sAMAccountName all lower case.
	strNewName = LCase(strNTName)
	' Make userPrincipalName all lower case.
	strNewPN = LCase(strUserPN)
	' Check if sAMAccountName should be modified.
	boolNeedToChange = False
	'If strNewName = "rsampson" Then
		'MsgBox strNTName & VbCrLf & strNewName & VbCrLf & strUserPN
		If strNTName <> strNewName Or strUserPN <> strNewPN Then
			boolNeedToChange = True
		End If
		If boolNeedToChange = True Then
			' Bind to the user object.
			Set objUser = GetObject("LDAP://" & strUserDN)
			' Assign new value for sAMAccountName.
			objLogFile.WriteLine VbCrLf & "Setting " & strNTName & " to " & strNewName
			objUser.sAMAccountName = strNewName
			If strUserPN <> "" Then
				' Assign new value for userPrincipalName.
				objLogFile.WriteLine "Setting " & strUserPN & " to " & strNewPN
				objUser.userPrincipalName = strNewPN
			Else
				objLogFile.WriteLine strNewName & " has no userPrincipalName"
			End If
			' Save changes.
			objUser.SetInfo
		End If
	'End If
	' Move to the next record in the recordset.
	objRecordSet.MoveNext
Wend

objLogFile.WriteLine VbCrLf & "Script ended: " & Now
objLogFile.Close

' Clean up.
objRecordset.Close
objConnection.Close

MsgBox "Done"

Open in new window

Avatar of krishnasmtpport25
krishnasmtpport25

Hi,

You can use this below Powershell script to pull change the case of Firstname, Lastname and SAM account name to Lowercase.

It needs Active roles powersshell snap in to execute this script. You can download active roles cmdlet from

$users = Get-content C:\users.txt | Get-Qaduser | Select-Object FirstName,Lastname,SamAccountName,LogonName

foreach ($user in $users)
{
[string]$Fname = $user.FirstName
$FName = $FName.ToLower()

[string]$Lname = $user.Lname
$Lname = $Lname.ToLower()

[string]$SamName = $user.$SamAccountName
$SamName = $SamName.ToLower()

get-Qaduser -identity $LogonName -Firstname $FName -Lastname $Lname -SamAccountName $SamName
}



Regards,
Krishna
Avatar of rsnellman

ASKER

OK, I will give this a try on a couple of test accounts and let you know the results.

Thanks again.

Bob
Remember that my script does the samAccountName, not the Display Name, First Name, and Last Names that you have requested.  Don't forget to change the script accordingly.

If you need assistance with that, I can do it for you.

Regards,

Rob.
Oh, and for testing, in my code, you'll see i had this:
'If strNewName = "rsampson" Then
.....
'End If

If you want to uncomment those lines, you can specify a specific logon name to only run against.

Regards,

Rob.
Hi,...sorry it has been a while since getting back to you all.  You know how the holidays are and then school starting back up and all that...time gets away from you.

Anyways, I am ready to try this.  Now Rob this script is vbscript, correct?  Remember I am far from a scripter and it looks sort of familar...so I would say it is, but want to make sure.  Now the only part of the accounts I am concerned about is the Full Name and maybe the display name.  The rest can stay the same as are now.

So, this script will do that for me?

Thanks again.

Bob
My script is VBScript, yes, but it only lowercases the samAccountName at the moment....I'll change it to do the full name shortly.....

Rob.
OK, thanks.  Yes, I need the fulname or aka cn to be lowercased.  The rest, like First Name, Last Name can remain uppercased.

Thanks again.

Bob
From what I can remember, CN is actually a different attribute to Display Name.  You can change DisplayName fine, but to change the CN, you actually to "rename" the account. I'll test it out and let you know.

Rob.
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for everything.

Bob