I wrote a script which is doing net use
at the beginning and net use /DELETE
at the end.
But if user decides to press Ctrl +
I don't think this is possible. At the beginning of your script, you can use:
net use /delete 2>nul
net use g: \\server\sharename /persistent:no
Or you could try pushd
instead of net use
...
If Command Extensions are enabled the PUSHD command accepts
network paths in addition to the normal drive letter and path.
If a network path is specified, PUSHD will create a temporary
drive letter that points to that specified network resource and
then change the current drive and directory, using the newly
defined drive letter. Temporary drive letters are allocated from
Z: on down, using the first unused drive letter found.
This way, you will always have mapped drive correctly set.