Hey, Scripting Guy! How can I rename a user account in a Windows NT 4.0 domain?

-- JK

SpacerHey, Scripting Guy! AnswerScript Center

Hey, JK. You know, as this column is being written it’s a Monday here in Seattle, and it’s raining. And yes, we know what you’re thinking: when isn’t it raining in Seattle? Unfortunately, though, our weather is much crueler than that. After all, if it rained all the time you could plan for that and get used to it. In Seattle, though, we tend to have three or four absolutely gorgeous days: sunshine, blue skies, highs in the 80s. It’s only then, just about the time you think that summer has arrived, that it rains. And rains. And, well, rains.

And then, of course, it gets nice for a couple of days, we start to get our hopes up, and then the same vicious cycle repeats itself.

Of course, there are two things you can do when the weather acts up like this. One thing you can do is sit around and mope, bemoaning the fate that has befallen you and doing as little work as possible. On the other hand, you can always take advantage of the opportunity and - with nothing to distract you - tackle one of those challenging problems that you’ve been meaning to tackle for a long, long time.

We’ve chosen to mope.

Set objDomain = GetObject("WinNT://fabrikam") 
Set objUser = GetObject(""WinNT://fabrikam/kmyer") 
objDomain.MoveHere objUser.AdsPath, "kenmyer"

You can see why we chose to answer this question: all we had to do was write three lines of code and then we could go back to feeling sorry for ourselves. We start off by creating a pair of object references, one to the domain (objDomain) and one to the user account that we want to rename (objUser). That’s what we do here:

Set objDomain = GetObject("WinNT://fabrikam") 
Set objUser = GetObject(""WinNT://fabrikam/kmyer")

And here’s the best part. After we’ve connected to the domain and to the user account it takes just one line of code to rename that account:

objDomain.MoveHere objUser.AdsPath, "kenmyer"

As you can see, all we do is call the MoveHere method and specify two parameters:

The AdsPath to the user account. The AdsPath uniquely identifies a user account in much the same way a UNC path uniquely identifies a file. For example, the UNC path \\atl-fs-01\public\test.txt can refer to only one file: a file named Test.txt found in the Public folder on the computer atl-ws-01. Likewise the AdsPath WinNT://fabrikam/kmyer can refer to only one user account: an account named kmyer located in the Fabrikam domain.

The new name (kenmyer) to be given to the user account.

And that’s it: call the method, and the kmyer user account will immediately be renamed kenmyer.

A few things to keep in mind here. First, neither Windows Script Host nor ADSI were available when Windows NT 4.0 first shipped. If you haven’t already done so this script won’t work unless you install both technologies on your domain controller. For more information, check out the Script Center FAQ.

Second, this script will work with Active Directory user accounts; it’s not limited to just NT 4.0 domains. We don’t really recommend you use this with Active Directory; with a few rare exceptions you should always use the LDAP provider (as opposed to the WinNT provider) when working with Active Directory. If you have no idea what we’re talking about, don’t feel bad: nobody ever has any idea what we’re talking about! But you can find a reasonably good explanation of ADSI providers (as well as which one to use, and when) in the Microsoft Windows 2000 Scripting Guide.

Finally, this script will not enable you to rename a local user account; instead it will fail with an “unspecified error” error message. So what do you do if you have the urge to rename local user accounts, at least on a Windows XP or a Windows Server 2003 computer? This Hey, Scripting Guy! column should help.

We should also mention that we agree with you: it is a bit odd that you use a method named MoveHere to rename a user account. Why don’t you just use something like, oh, say, Rename? That’s a question we can’t answer. The best we can do is tell you not to worry about it. The syntax might seem a little strange, but it works. And that’s all we really care about anyway.

In the meantime, let’s check tomorrow’s forecast:

Cloudy with showers. High 62F. Winds S at 10 to 15 mph. Chance of rain 60%.

Wonderful. Word of warning: if you thought today’s column was filled with self-pity just wait until you see tomorrow’s. We had a little bit of rain on Saturday, a lot of rain on Sunday, rain today, and a 60% chance of rain tomorrow. There’s just nothing like Seattle in the summertime, is there? (Editor’s Note: It’s actually still spring, which might explain this if it weren’t for the fact that summer works pretty much the same way.)