In a batch-file I use the following syntax to map a network drive:
NET USE N: \\someserver\somedirectory.
While this works fine, it also gives an error if the letter n is already used. Is it possible to suppress this error message?
Also, is there a way to unmap a network drive?
I imagine something like:
NET UNUSE N:
One method that works in all modern versions of Windows is to use the Command Prompt. Open it and type: net use drive letter /delete. Then, press Enter. For example, we have a drive mapping using the letter Z, so we have to type: net use Z: /delete.
Connects or disconnects your computer from a shared resource or displays information about your connections.
After
NET USE N: \\somserver\somedirectory
to remove simply:
NET USE N: /DELETE
To test for an existing mapping you can;
IF NOT EXIST N:\. GOTO BLABLA
You can detect if a drive is in use by running
net use N:
you'll get different return results $?
in PowerShell and %ERRORLEVEL%
in Batch.
If mapped $? will be True
and %ERRORLEVEL%
will be 0
To unmap, just do
net use N: /DELETE
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With