This article details how to map a network drive upon login using a registry entry and a batch file, without needing to store a password in plain text. The batch file that you create and upload to the command builds a netuse.bat file automatically to map drives when the user logs in to their device.
Prerequisites:
- This command works on Windows devices only.
- This method applies to file servers with NTFS permissions, AD file servers, and any NAS device, as long as the username and password on the device matches the username and password on the end user’s device.
Considerations:
- The example used in this article will:
- Create a registry entry key named mapdrive that will run a script (c:\scripts\netuse.bat) every time a user logs in.
- Delete any old version of c:\scripts\netuse.bat that exists.
- Generate the netuse.bat file that contains the actual drive map commands and save it to c:\scripts\.
- The example variables should be edited to specify the desired drive letter, file server/NAS address, and share name.
- Create the batch file (setnetdrive.bat), replacing the variables as necessary:
echo off
reg add hklm\software\microsoft\windows\currentversion\run /v mapdrive /t REG_SZ /d c:\scripts\netuse.bat /fmkdir c:\scripts
echo net use * /delete /yes > c:\scripts\netuse.bat
echo net use f: "\\fileserver\share" /user:%%username%% >>
echo net use g: "\\fileserver\share" /user:%%username%% >>
c:\scripts\netuse.bat
If the file server is an Active Directory member file server, then add the DOMAIN NAME before the %%username%% variable:
echo net use f: "\\fileserver\share" /user:DOMAIN\%%username%%
For example, if the file server is 10.10.10.10, share name is “Finance,” and AD domain is “Contoso.Local” the command would look like this:
echo net use f: "\\10.10.10.10\Finance" /user:CONTOSO\%%username%%
- In the JumpCloud Admin Portal, go to DEVICE MANAGEMENT > Commands.
- Click + and choose Command.
- Enter the following Details:
- For Name, enter a name that is identifiable to you.
- For Type, select Windows.
- Ensure the Windows Powershell option remains unchecked.
- In the Command* field, enter
cmd.exe /c c:\windows\temp\setnetdrive.bat
.
- Under Launch Event, set the Event to Manually to run once per device.
- (Optional) Under Options, configure the command’s timeout settings appropriately, or leave the default settings.
- Under Files, click Upload File.
- Upload the setnetdrive.bat file you created in step 1. Ensure the File Destination is listed as c:\Windows\Temp\setnetdrive.bat.
- Assign the command to the device or device group where you want to apply the registry entry.
- From the Device Groups tab, select one or more device groups. For device groups with multiple OS member types, the policy is applied only to the supported OS.
- From the Devices tab, select one or more devices.
- Click Save.
- From the Commands list, find the command and click Run Now to execute the setnetdrive.bat file on all selected devices.
- The registry entry is applied and the netuse.bat file is built on the end device, and will be run every time a user logs into that device.
Additional Resources
Enroll: Running Commands
Blog: Accessing File Shares on AD-Connected Windows File Servers