Logging out of a network share drive without reboot?

All we need is an easy explanation of the problem, so here it is.

I have a small script that runs on each workstation which runs defrag and chkdsk and connect each workstation temporarily to a network share that they, normally, do not have access to. The command I use to connect to them is:

net use \\server\sharename [password] /USER:domain\username P:No

Is there a command to log that user out of the drive share without restarting the computer?

Workstations run on Windows XP SP3

EDIT: I’ve used:

net use \\server\sharename /delete

but the command returns “The network connection cannot be found”. However, I can still navigate to \\server\sharename in MyComputer address line and have full access to it still.

EDIT2: Assume I’ve read through the help files for net use ;D

EDIT3: Would a reboot suffice? Yes… however it is more convenient to log the user out of the share instead =D. I know I’m difficult.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

net use \\server\sharename /delete

To cause the credentials to get flushed:

net use \\server\IPC$ /delete

Method 2

If the net use commands listed here do not do the trick, instead of rebooting, you can just restart the Workstation service (I’m not sure how to do this without the service restart though):

  1. First use the appropriate net use commands as above (e.g. net use * /del for the shotgun approach).
  2. Control Panel -> Administrative Tools -> Services
  3. Find Workstation in the list.
  4. Right click and choose Restart.

If you have any explorer windows open for the shared folder, or any open files, you may have to close them in order for the service to restart (if the restart appears to hang this is usually why).

Alternatively you can restart the service on the command line with:

net stop Workstation
net start Workstation
net start "Computer Browser"

The Computer Browser start is usually necessary as stopping Workstation also stops Computer Browser. You’ll see if it does in the output from the first command. Again, closing explorer windows and open files may be necessary.

Reference: https://social.technet.microsoft.com/Forums/windows/en-US/58494ea1-e3a0-49ab-be5c-1aa2b86f13c9/how-to-logout-from-shared-folder

Method 3

net use \\server\sharename /delete

See the full net use command here.

Method 4

@rocketsarefast and I have discovered another way to clear the network credentials. I have posted my answer in this thread:

I have also discovered from @rocketsarefast’s answer that Windows will clear the old network credentials when there is a new login attempt.

However, his net use "\\\c$" "badpassword" /user:"baduser" command is way too slow, especially when the client has to wait up to several seconds for the server to respond with a rejection, which is terrible and inconvenient.

For my use case, I was able to use the Win32 API to map the network share \\Server\Share to the S:\ drive. It disconnects the drive first and then prompts the user with the Windows network login dialog so that the person can logout and login as a different user.

Here is the C code main.c:

#ifndef UNICODE
#define UNICODE

#define WINVER 0x0500
#define _WIN32_WINNT 0x0500
#define _WIN32_IE 0x0500

#pragma comment(lib, L"mpr.lib")

#include <windows.h>
//#include <stdio.h>

int wmain(const size_t argc, const wchar_t** argv) {
    NETRESOURCE networkResource = {0};
    networkResource.dwType = RESOURCETYPE_DISK;
    networkResource.lpLocalName = L"S:";
    networkResource.lpRemoteName = L"\\\\Server\\Share";
    networkResource.lpProvider = NULL;

    DWORD result = 0;
    result = WNetCancelConnection2(networkResource.lpLocalName, CONNECT_UPDATE_PROFILE, TRUE);
//    wprintf(L"WNetCancelConnection2 result: %d\n", result);

    result = WNetAddConnection2(&networkResource, NULL, NULL, CONNECT_INTERACTIVE | CONNECT_PROMPT);
//    wprintf(L"WNetAddConnection2 result: %d\n", result);

//    getchar();

    return EXIT_SUCCESS;

Here is the CMakeLists.txt:

cmake_minimum_required(VERSION 3.7)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -g3 -pedantic -Wall -Wextra -O0")

set(SOURCE_FILES main.c)
add_executable(MapNetworkDrive ${SOURCE_FILES})
target_link_libraries(MapNetworkDrive mpr.lib)

Compile using MinGW-w64 – for 32 and 64 bit Windows:

As an alternative to C, here is an easy C# tutorial on the API:

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply