Apr 242014

Recently at my place of work the question was raised about adding a friendly name to a drive mapping for workstations attached to a our domain. Now normally we map drives using three different methods depending on the workstation in question and partly from legacy systems.

Going back ten years when it was all Windows XP and Server 2003, drive mappings were applied using a login script in Group Policy (GPO).

Then came Windows 7 and Server 2008 along with Active Directory 2008 (AD) and you could now map drives in GPO and apply a friendly name against the mapping. In Windows 7, instead of the user seeing “sharename (\\server) (G:)” they would see “FriendlyName(G:)”. This works really well and we apply friendly names to all our group and department drives.

However the one drive mapping that would never get a friendly name applied is the one created for the users personal directory (look in AD at the Profile tab on a users account for the “Home Folder” section).

The below script will apply the DisplayName for a user from their AD details and apply it as the FriendlyName for the users home drive. You only need to apply the script once as the FriendlyName for the mapped drive is then held in the local registry for the workstation.

'Version 1.0 24th April 2014 - Nick Woodruffe - www.pyrobrit.co.uk
'Returns the users DisplayName in Active Directory and applies it
'as the FriendlyName to the users Personal drive in this case it is the "P:" Drive.

option explicit

Dim mdrive, oshell, fso, objNetwork, objUser
Dim oADSystemInfo
Dim strUserDN, strUserName, strUserNameMid, strUserNameLong, strUser

Set fso=CreateObject("Scripting.FileSystemObject")
Set objNetwork = WScript.CreateObject("WScript.Network")
Set oADSystemInfo = CreateObject("ADSystemInfo")
strUserDN = Replace(oADSystemInfo.UserName,"/","\/")
Set objUser = GetObject("LDAP://" & strUserDN)

'Strip out the users DisplayName from long string returned in objUser.AdsPath
'There is probably a more efficient way of picking out the DisplayName using one string statement
'but by spliting over three lines and adding a Wscript.Echo between each line will show
'you what each line is doing should you wish to see it.
strUserNameLong = objUser.AdsPath
strUserNameMid = right(strUserNameLong, len(strUserNameLong) - instr(strUserNameLong,"="))
strUserName = left(strUserNameMid, (instr(1,strUserNameMid,",")-1))

'Rename User Home Directory
if fso.driveexists("P:") then
    mdrive = "P:\"
    Set oShell = CreateObject("Shell.Application")
    oShell.Namespace(mdrive).Self.Name = strUserName
end if

There is one other method that we use for mapping drives and applying FriendlyNames to them. We use a product called RES Workspace Manager. It has a steep implementation curve but has much more functionality than plain old GPO. I will not say anything more about RES for the moment but if you have heard of it or you want a tool to make your Admin of AD more efficient then I thoroughly recommend it.

 Leave a Reply