I would to logoff a user using command line witch should be done using username not session id it should be done forcefully
i tried : shutdown -l but it works only for the current user also tried logoff command but it require a session name
EDITED : i have tried a logoff script but it's not working.. any one can fix it ?
BATCH FILE
@echo off
query session > logoff.txt
for /f "skip=2 tokens=2,3" %%i in (logoff.txt) DO if [%%i]==[%1] logoff %%jLOGOFF.TXT
SESSIONNAME USERNAME ID STATE TYPE DEVICE services 0 Disc console 1 Conn
>rdp-tcp#0 Administrator 2 Active hi 3 Disc h 4 Disc Abdou 5 Disc Abdou76 6 Disc rdp-tcp 65536 Listen TRYING THE SCRIPT.. BUT FAILED
C:\dir>logoff.bat Abdou76
C:\dir>User Abdou76 Still Loged in
42 Answers
Batch:
There is no in-built Windows "logoff by name" utility that I'm aware of.
You could use a batch file to query the current sessions, pick out the names and session IDs, and then logoff matching users:
@echo off
query session > sessioninfo.txt
for /f "skip=2 tokens=2,3" %%i in (sessioninfo.txt) DO if [%%i]==[%1] logoff %%j
del sessioninfo.txtUsage:
batchfile.bat username
The batch loads the results of query session into a text file.
It then uses a for loop, skipping the first two lines, to load the username and session ID tokens for each line into I and J.
I is checked to see if it matches the name provided as an argument (represented by %1), and if so logoff is used with the corresponding session ID.
After some comments and details regarding "disconnected" users, I'm thinking this would be better done in PowerShell.
PowerShell:
Here's a PowerShell script that will logoff all sessions that match the user name you feed it, and this works if the user if active, or disconnected.
param ( [string]$username = $(throw "-username is required.")
)
function Get-Sessions
{ $queryResults = query session $starters = New-Object psobject -Property @{"SessionName" = 0; "UserName" = 0; "ID" = 0; "State" = 0; "Type" = 0; "Device" = 0;} foreach ($result in $queryResults) { try { if($result.trim().substring(0, $result.trim().indexof(" ")) -eq "SESSIONNAME") { $starters.UserName = $result.indexof("USERNAME"); $starters.ID = $result.indexof("ID"); $starters.State = $result.indexof("STATE"); $starters.Type = $result.indexof("TYPE"); $starters.Device = $result.indexof("DEVICE"); continue; } New-Object psobject -Property @{ "SessionName" = $result.trim().substring(0, $result.trim().indexof(" ")).trim(">"); "Username" = $result.Substring($starters.Username, $result.IndexOf(" ", $starters.Username) - $starters.Username); "ID" = $result.Substring($result.IndexOf(" ", $starters.Username), $starters.ID - $result.IndexOf(" ", $starters.Username) + 2).trim(); "State" = $result.Substring($starters.State, $result.IndexOf(" ", $starters.State)-$starters.State).trim(); "Type" = $result.Substring($starters.Type, $starters.Device - $starters.Type).trim(); "Device" = $result.Substring($starters.Device).trim() } } catch { $e = $_; throw "ERROR: " + $e.PSMessageDetails } }
}
$username = $username.ToLower()
$userSessions = Get-Sessions | ? { ($_.UserName).ToLower() -eq $username } | Select ID, UserName
$numberOfSessions = ($userSessions | measure).Count
if ($numberOfSessions -gt 0) { foreach ($session in $userSessions) { $sessionInfo = $session.Username + " (" + $session.ID + ")" Write-Host "Found $sessionInfo" logoff $session.ID /V }
} else { Write-Host """$username"" not found in session list."
}Usage (from within PowerShell):.\LogEmOff.ps1 Abdou76
The user name argument is intentionally NOT case sensitive (i.e.: AbDoU76 = Abdou76 = ABDOU76 = abdou76)
5i have found the solution : using batch code
@echo off
query user > logoff.txt
for /f "tokens=1,2" %%i in (logoff.txt) DO if /I [%%i]==[%1] logoff.exe %%jUsage :
logoff username
NOTE : The user name argument is intentionally NOT case sensitive (i.e.: username = UserName = USERNAME = USERname)