8

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.

4
  • 1
    Have you tried Net Use * /Del? Aug 25, 2011 at 17:20
  • we other user shares associated to the login, and we do not wish to remove all of them. Just the one that we've temporarily allowed the user access to. Aug 25, 2011 at 17:22
  • I (mostly) meant for testing purposes. But I think I see what's going on here, so I've added an 'answer'. :) Aug 25, 2011 at 17:37
  • See my answer here. You may also need to do klist purge.
    – mivk
    Dec 24, 2020 at 10:27

4 Answers 4

6
net use \\server\sharename /delete

EDIT: To cause the credentials to get flushed:

net use \\server\IPC$ /delete
6
  • hmm, usually SU notifies me sooner if an answer is added when I'm typing, +1 for being first.
    – MaQleod
    Aug 25, 2011 at 16:02
  • 3
    I've tried this, but the command returns "The network connection cannot be found." However, I can still access the network share freely.\ Aug 25, 2011 at 17:02
  • I'm so dumb... double checked my code this morning and found I had a trailing \ on after the sharename... tested it and yep the trailing slash makes a difference. Sorry for my n00bizm Aug 26, 2011 at 13:08
  • 3
    I tried net use \\server\IPC$ /delete and it gives the error The network connection could not be found. Sep 23, 2013 at 15:16
  • 1
    net use * /del can be used as a shotgun approach if you don't have any other connections you mind destroying (net use by itself will list them). After that, if it still doesn't work, restart the Workstation service instead of rebooting.
    – Jason C
    Feb 28, 2015 at 1:57
4

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

1
  • net start "Computer Browser" returns The service name is invalid. Has the service name changed? Apr 2, 2020 at 19:17
3
net use \\server\sharename /delete

See the full net use command here.

0

@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 "\\10.0.0.5\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
#endif

#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)
project(MapNetworkDrive)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c11 -g3 -pedantic -Wall -Wextra -O0")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} ${GCC_COVERAGE_LINK_FLAGS} -municode -mwindows")

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:

1
  • Please do not post the same answer to multiple questions. If the same information really answers both questions, then one question (usually the newer one) should be closed as a duplicate of the other. You can indicate this by voting to close it as a duplicate or, if you don't have enough reputation for that, raise a flag to indicate that it's a duplicate. Otherwise tailor your answer to this question and don't just paste the same answer in multiple places.
    – DavidPostill
    Jun 4, 2017 at 9:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .