batch file to add map driver for network location and ask for credential

Temody Mas
Temody Mas used Ask the Experts™
on
Hello Experts
I need a batch file or script to add map driver for network location with letter x for example and ask for credential
and another batch or script to remove that mapped drive and credential for that network address
The scenario information
server IP: 192.168.1.254
shared folder on that IP: Home
Domain name: Private.local
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Batch (works as well in PowerShell) to map X: (you actually don't even need a batch file - you could add net.exe and its arguments as target for a shortcut):
net.exe use X: \\Server\Share * /user:SomeDomain\SomeUser /persistent:no

Open in new window

Remove the drive:
net.exe use X: /delete

Open in new window

Author

Commented:
Thanks
it's working great, but when i run remove command and then run connect command again the drive mapped without any authentication
what i need  in remove command is to remove saved credential for that address as well even without user logoff needed
Most Valuable Expert 2018
Distinguished Expert 2018
Commented:
There's some non-persistent caching going on.
Try this to remove the drive; you can replace "foo" with any string you want (the password), and "bar" with yet another string (but not a valid username) you want:
net.exe use X: /delete & net.exe use X: \\Server\Share foo /user:bar /persistent:no >NUL 2>&1

Open in new window

Author

Commented:
Thanks

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial