?
Solved

How to remove underscore from user accounts

Posted on 2009-05-11
6
Medium Priority
?
654 Views
Last Modified: 2013-11-25
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.
0
Comment
Question by:cjrcomputers
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24354834

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
0
 
LVL 1

Author Comment

by:cjrcomputers
ID: 24354850
This is the username.
0
 
LVL 71

Expert Comment

by:Chris Dent
ID: 24355028

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

0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 1

Author Comment

by:cjrcomputers
ID: 24355054
I believe I want to change both.
0
 
LVL 71

Accepted Solution

by:
Chris Dent earned 2000 total points
ID: 24355600

Okie dokie. This does both then. The other is called "userPrincipalName" in the directory, it should be in the form "someone@yourdomain.com".

If you're happy with it, remove the comment (') from these lines:

      ' objUser.Put "sAMAccountName", strNewUsername
      ' objUser.Put "userPrincipalName", strNewUPN
      ' objUser.SetInfo

Then I suggest you run it on a small / test set of users to start off with.

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")
    strOldUPN = objUser.Get("userPrincipalName")
    If InStr(strOldUsername, "_") > 0 Then
 
      ' Remove the Underscore
      strNewUsername = Replace(strOldUsername, "_", "")
      strNewUPN = Replace(strOldUPN, "_", "")
 
      ' Pop up a box telling us about the change
      WScript.Echo "sAMAccountName: Old " & strOldUsername & _
        " New " & strNewUsername & vbCrLf & _
        "userPrincipalName: Old " & strOldUPN & _
        " New " & strnewUPN
 
      ' Once we're happy the script does as it should uncomment these:
      ' objUser.Put "sAMAccountName", strNewUsername
      ' objUser.Put "userPrincipalName", strNewUPN
      ' objUser.SetInfo
    End If
 
  End If
Next

Open in new window

0
 
LVL 1

Author Comment

by:cjrcomputers
ID: 24355781
Worked great!

Thanks.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Active Directory security has been a hot topic of late, and for good reason. With 90% of the world’s organization using this system to manage access to all parts of their IT infrastructure, knowing how to protect against threats and keep vulnerabil…
Here's a look at newsworthy articles and community happenings during the last month.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question