How Can I Assign a New Home Drive to a User Who Doesn’t Have One?


Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I check to see if a user has a home drive and home directory set and, if they don’t, assign them a new one?

-- AM

SpacerHey, Scripting Guy! AnswerScript Center

Hey, AM. No, no excuses: the Seahawks had plenty of opportunities to win the Super Bowl yesterday, but they failed to take advantage of it. At times it seemed like they were playing not to lose instead of playing to win, and that’s always a recipe for disaster. Oh, well; that’s the way the world goes round.

Of course, considering how far they had to travel to get there you’d think that the officials would have been interested in watching the game themselves. But apparently not. Matt Hasselbeck gets called for blocking below the waist while making a tackle? OK ….

No, that’s not an excuse: it’s just an … observation ….

But, hey, what’s done is done, and, besides, this is supposed to be a scripting column, not a sports column. With that in mind, how can we test for the existence of a home directory and, if necessary, assign a new home directory and home drive for the user? Well, this should do the trick:

On Error Resume Next

Set objUser = GetObject("LDAP://cn=Ken Myer, ou=Finance, dc=fabrikam, dc=com")

If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then
strUser = objUser.sAMAccountName
strHomeDirectory = "\\atl-dc-01\users\" & strUser
objUser.homeDirectory = strHomeDirectory
objUser.homeDrive = "Z:"
End If

In this script we begin by binding to the Ken Myer user account in Active Directory. That brings us to this line of code:

If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then

As you probably know, Active Directory scripting can be a tad bit tricky from time-to-time. Suppose you look at a user account in Active Directory Users and Computers and see that the home directory box is blank. In that case, your first thought is likely to be, “Oh, I guess this user doesn’t have a home directory.” And you would be right.

If you run a script against this same account, however, you have to concern yourself with two things: is the home directory box blank because the attribute is null (that is, no value has been assigned to it), or is the home directory box blank because the attribute is empty (perhaps because another administrator ran a script that set the value to an empty string)?

Yes, we know: an empty attribute and a Null attribute would seem to be the same thing. For better or worse, though, they aren’t. Technically speaking, an empty attribute actually has a value: it’s just that the value is nothing. With a Null attribute, however, we’re saying that we have no idea whether or not this property has a value. It’s confusing, but it’s an important distinction. If the homeDirectory attribute is empty that means that the user doesn’t have a home directory assigned. However, in that case the following test will return False and lead us to believe that the user does have a home directory:


Why does this return False? Well, because the attribute isn’t Null, it’s empty. And, like it or not, there’s a difference

That’s all right: let us know when your head stops spinning and then we’ll continue.

Better? To be honest, this isn’t worth worrying about, at least not right now. Instead, just play it safe and do what we did: check to see if the attribute value is empty or if it’s Null. You can do that by using both the IsEmpty and IsNull functions in your If-Then statement:

If IsEmpty(objUser.homeDirectory) or IsNull(objUser.homeDirectory) Then

This way you’re covered no matter what.

So suppose the user doesn’t have a home directory; what then? Well, then we execute this block of code:

strUser = objUser.sAMAccountName
strHomeDirectory = "\\atl-dc-01\users\" & strUser
objUser.homeDirectory = strHomeDirectory
objUser.homeDrive = "Z:"

Home directories are typically named after the user. (They don’t have to be, but that’s the standard adopted in many organizations.) Because we’re too lazy to come up with our standard, we’re going to use this same approach; that means we’re going to assign the user the home directory \\atl-dc-01\users\logonname, where logonname represents the user’s logon name (sAMAccountName). Granted, we could hard-code this name into the script. However, we decided to get a tiny bit fancy and have the script create the home directory name for us.

To do that we first assign the value of the user’s sAMAccountName (for example, kenmyer) to a variable named strUser. We then use this line of code to construct the UNC path to the directory:

strHomeDirectory = "\\atl-dc-01\users\" & strUser

If the user’s sAMAccountName really is kenmyer then the resulting path (and the value assigned to the variable strHomeDirectory) will be this:


Once we have the complete path we can set the value of the homeDirectory attribute; after that we‘re free to set the value of the homeDrive attribute to Z:. (And, yes, you must include the colon following the drive letter.) And when all that’s done we can call SetInfo to write the changes back to the user account. Whatever you do, don’t leave out the call to SetInfo: if you do your changes will not be applied to Active Directory. Which in turn means the user still won’t have a home drive and directory.

So are we really putting away the sports references forever? Of course not: after all, the end of the Super Bowl simply means that baseball season is just around the corner. And this year we think that our very own Seattle Mariners are going to -

On second thought, we’ll get back to you about those sports references ….

Comments (2)

  1. Sunil says:

    how can I assign this to all users in one OU.

    for example there are 10 users in  OU=test,dc=home,dc=local and I want a home drive or home directory created with their samaccountname. how can I achive this?

Skip to main content