Override mapped drive name using .bat script?

Solution 1:

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

Solution 2:

There is no way to do this using only the net use command (see documentation), but there is a way to do this using a vb script, as described by Guy Thomas at computerperformance.co.uk here

In case his Guy's site disappears later, here is his a copy of his script:

' NameDrive.vbs
' VBScript to map a network drive.
' Authors Guy Thomas and Barry Maybury
' Version 1.4 - April 2010
' ----------------------------------------'
'
Option Explicit
Dim objNetwork, strDrive, objShell, objUNC
Dim strRemotePath, strDriveLetter, strNewName
'
strDriveLetter = "W:"
strRemotePath = "\\192.168.1.2\example\sharename"
strNewName = "Example Readable Label"

' Section to map the network drive
Set objNetwork = CreateObject("WScript.Network")
objNetwork.MapNetworkDrive strDriveLetter, strRemotePath

' Section which actually (re)names the Mapped Drive
Set objShell = CreateObject("Shell.Application")
objShell.NameSpace(strDriveLetter).Self.Name = strNewName

Wscript.Echo "Check : "& strDriveLetter & " for " & strNewName
WScript.Quit

' End of Example VBScript.

Note:

  • If W: doesn't work for you, try W:\ (with the slash)
  • This method will set the drive letter label permanently, i.e. if you later connect another share to the same drive letter, that new share will also get the old label. This can be fixed by either always using this script when connecting shares, or by deleting a key in the registry to revert to normal behaviour.

Reverting to normal behaviour:

  • Launch regedit and then click on Edit (menu) -> Find -> Example Readable Label
  • Or navigate in regedit to HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\MountPoints2 -> _LabelFromReg
  • Just delete the value - leave it blank. The result will be that future drive mapping will revert to the traditional style of mapping.

All of this is more thouroguhly described at Guy's website.