NET.exe - NET SESSION and NET FILE

The NET command is used to manage open files and user sessions as follows:

Display all the open shared files on a server and the lock-id
NET FILE

Close a shared file (disconnect other users and remove file locks)
NET FILE id /CLOSE

List all sessions connected to this machine:
NET SESSION
or
NET SESSION | Find "\\"

List sessions from a given machine
NET SESSION \\ComputerName

Disconnect all sessions connected to this machine
NET SESSION /DELETE

Disconnect all sessions connected to this machine (without any prompts)
NET SESSION /DELETE /y

Disconnect sessions from a given machine
NET SESSION \\ComputerName /DELETE

NET SESSION displays incoming connections only, in other words it must be run on the machine that is acting as the server.

To create file shares the SERVER service must be running, which in turn requires 'File and Print Sharing' to be installed.

UAC elevation

Administrator privileges are required to run the NET SESSION command. We can use this fact to detect if the current user is an Admin as follows:

:: Check Admin/Elevated permissions
Net Session >nul 2>&1
If %errorLevel% NEQ 0 (
   Echo Failure, please rerun this script from an elevated command prompt. Exiting...
   Ping 127.0.0.1 2>&1 > nul
   Exit /B 1
) else (
   Echo Success: Admin permissions confirmed.
)

NET SESSION does require the Server service to be running, two alternative methods (via StackOverflow) are:

fsutil dirty query %systemdrive% >nul
or
sfc 2>&1 | find /i "/SCANNOW" >nul

“Never mistake activity for achievement” ~ John Wooden

Related:

NET - Manage network resources
NET USE - connect to a file share
OPENFILES - List or disconnect open files, local or remote (Win XP)
PsFile - Show files opened remotely
PsLoggedOn - Who's logged on.
SS64 Forum thread on UAC detection.
Q149427 - Change Password from the CMD prompt
Q321711 - DrMapSrv DriveShare utility for Win 2K Terminal Services
VBScript - Check for Admin/Elevation
Powershell: Get-WmiObject win32_share
Equivalent bash command (Linux): who currently logged in


© Copyright SS64.com 1999-2014
Some rights reserved