Windows – Override mapped drive name using .bat script

batch filerenamescriptwindows

I have the following batch script which is successfully mapping a few of our drives for our users:

@echo off
net use * /delete /yes
net use x: \\192.168.1.52\xrays
net use s: \\192.168.1.52\common
net use p: \\192.168.1.52\public
net use o: \\192.168.1.52\office
net use y: \\192.168.1.52\drives
EXIT

The mapped drives are taking their names from the share name themselves. However, it would be really handy if I could override the name to something more useful to the users.

I have looked at quite a few documents online with examples of the net use commands, but I can only see options such as credentials but nothing to do with the naming.

The script is being run on Windows XP and Windows 7 workstations.

Any help would be appreciated.

Best Answer

There is way to do it from the command line without using VBScript. You can edit the registry using the reg add command. IMHO, doing it this way will be better than using VBScript to change the label because it will not associate the label with the drive letter, but rather it will associate the label with the share. So, if the end user later disconnects X: and manually mounts the xrays share to say the R: drive, then the label will still show up correctly (to whatever you assigned it in the script).

The key you will be writing to is HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\ with the sub-key being the share path with all of the backslashes replaced with pound symbols (#).

Note: I have not yet tested how you should handle share names with spaces (or even pound symbols) in them.

@echo off

net use * /delete /yes

:: Set the label in the registry
reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#xrays /v _LabelFromReg /t REG_SZ /f /d "X-Rays"
:: Map the drive
net use x: \\192.168.1.52\xrays

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#common /v _LabelFromReg /t REG_SZ /f /d "Common"
net use s: \\192.168.1.52\common

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#public /v _LabelFromReg /t REG_SZ /f /d "Public"
net use p: \\192.168.1.52\public

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#office /v _LabelFromReg /t REG_SZ /f /d "Office"
net use o: \\192.168.1.52\office

reg add HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2\##192.168.1.52#drives /v _LabelFromReg /t REG_SZ /f /d "Drives"
net use y: \\192.168.1.52\drives

EXIT
Related Question