Link to home
Start Free TrialLog in
Avatar of cjrcomputers
cjrcomputers

asked on

How to remove underscore from user accounts

When we first setup our Exchange Organization we setup all the user accounts with an underscore between the first initial last name.  (Ex: j_doe.) We are looking to remove that underscore in active directory and I'm wondering if there is a script to automatically remove underscore in user accounts within a specific OU.
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


Yes, but it depends exactly where it appears. Is this the username? Or name you see in the list in AD Users and Computers? Or DisplayName?

Chris
Avatar of cjrcomputers
cjrcomputers

ASKER

This is the username.

Okay, one more extension on that. The pre-Windows 2000 login name? Or the other? :)

If it's the "pre-Windows 2000 login name", which is the most popular, then this little VbScript below will change it for a single OU. You would have to specify the OU on the first line.

It doesn't change anything at the moment, just tells us about it.. for testing / approval.

Chris
Set objOU = GetObject("LDAP://OU=Somewhere,DC=yourdomain,DC=com")
objOU.Filter = Array("user")
 
For Each objUser in objOU
  ' Prevent it catching computers with user objectClass
  If InStr(objUser.Get("objectCategory"), "Person") > 0 Then
 
    ' See if there's an Underscore in the Username
    strOldUsername = objUser.Get("sAMAccountName")
    If InStr(strOldUsername, "_") > 0 Then
 
      ' Remove the Underscore
      strNewUsername = Replace(strOldUsername, "_", "")
 
      ' Pop up a box telling us about the change
      WScript.Echo "sAMAccountName: Old " & strOldUsername & _
        " New " & strNewUsername
 
      ' Once we're happy the script does as it should uncomment these:
      ' objUser.Put "sAMAccountName", strNewUsername
      ' objUser.SetInfo
    End If
 
  End If
Next

Open in new window

I believe I want to change both.
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland 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
Worked great!

Thanks.