Batch file to map a network drive, with ability to enter credentials and mask passwords

Following is a batch file which allows you to map a network drive, but it also includes ability to enter credentials and mask passwords. In this example, script will: Prompt...

Following is a batch file which allows you to map a network drive, but it also includes ability to enter credentials and mask passwords.

In this example, script will:

- Prompt the user to enter their server access credentials, more specifically their network **User name** and **Password** (masked with asterix * symbol)

- Disconnect any existing X drive connections/mappings

- Map the network drive as X drive using entered credentials

- Pause for 3 seconds, before opening X drive in Windows Explorer to confirm that X drive is mapped and accessible
@echo off

set /p id="User name: "

powershell -Command $pword = read-host "Password" -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword) ; ^
        [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR) > .tmp.txt 
set /p password=<.tmp.txt & del .tmp.txt

net use x: /delete
net use x: \\server-IP-address\folder /User:%id% "%password%" /persistent:yes

TIMEOUT 3

%SystemRoot%\explorer.exe "x:\"

Enjoy!