Batch file to map an external network drive and prompt for username and password

PhoenixJay picture PhoenixJay · Feb 29, 2016 · Viewed 21.8k times · Source

I am fairly new to BAT File Scripting and I was wondering if someone could tell me and show me if it is possible to create a script that will map a network drive and prompt the user to enter their username and password as well as have it save their credentials so it does not prompt them every time they try to access the network drive? I read a few post here about using net use but I really didn't find anything to my situation. Only reason I am asking for this is because we have about 60 employees here and I really don't feel like mapping the drive one by one.

I have been working on this and this is what I have so far but it is not working for me. Is it because it is an external network?

@echo off
net use z: /delete
echo PLEASE ENTER YOUR USER ID AND PRESS [ENTER]
set /p USERID=
echo PLEASE ENTER YOUR PASSWORD AND PRESS [ENTER]
set /p PASSWORD=

pause
net use Z: "\\SERVER.DOMAIN.ORG\workgroup" /USER:AZTUC\%USERID%
%PASSWORD% /PRESISTENT:YES

Answer

PhoenixJay picture PhoenixJay · Mar 1, 2016

I was able to figure this out and I want to post the answer because I am sure there are many others with the same question. Since I am mapping an external network drive, I added a cmdkey.exe command to add the user credentials to the Credential Manager, once it was added I then net use the network drive and folder to the Z drive. Worked like a charm. Hope this helps.

@echo off
if exist net use z: /delete

echo Please enter User ID:
set /p USERID=

echo Please enter your Password:
set /p PASSWORD=

cmdkey.exe /add:*.sample.com /user:DOMAIN\%USERID% /pass:%PASSWORD%
net use Z: "\\SERVER.DOMAIN.COM\workgroup"
pause