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

24,596

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
Share:
24,596
PhoenixJay
Author by

PhoenixJay

Updated on November 29, 2022

Comments

  • PhoenixJay
    PhoenixJay over 1 year

    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
    
  • Admin
    Admin almost 6 years
    Good start @pheonixJay when I use your script but I get the system error 53 which means what? The patch of my drive is wrong ? One more thing. Do I have to change something is the cmdkey.exe line? As you notice, I am not familiar with batch file. Thanks for the help.
  • PhoenixJay
    PhoenixJay almost 6 years
    Hi @Doum , sorry for the late response, I was having issues logging into my Stack account. System error 53 usually results to the server name and folder you are trying to path to, it could be a credential issue or naming issue. If you are using the name make sure you have all the correct spelling and domain detail, or try to use the IP Address. Verify first in the mapping function that you can map the drive as well as access it with you given credentials and have permission to get into the shared drive. Hope this helps. Again, sorry for the late response.