Batch – Return User Session Information

email me

This will return the user’s session information locally, and remotely. I use this with SCCM and Intune.

 

Code.cmd

 

:: MrNetTek
:: eddiejackson.net
:: 7/11/2024
:: free for public use
:: free to claim as your own

@ECHO OFF

:: Initialize
SETLOCAL enabledelayedexpansion
SET tstQUser=false
SET tstQWin=false

:: Detect Session
:: Method 1
quser | find /i "%USERNAME%" > nul
IF %ERRORLEVEL% == 0 (
   FOR /f "tokens=3" %%i IN ('quser') DO SET quserSessId=%%i
      ECHO QUser: %username% !quserSessId!
      SET tstQUser=true
      ECHO.
   )
:: Method 2
FOR /f "tokens=1-6" %%a IN ('qwinsta') DO (
   IF "%%b" == "%USERNAME%" (
      SET quinstaSessId=%%c
      ECHO QWinsta: %username% !quinstaSessId!
      SET tstQWin=true
      ECHO.
   )
)

:: Display Output
ECHO QUser: %tstQUser%
ECHO QWinsta: %tstQWin%
ECHO Username: %username%
ECHO Session Id: %quserSessId%
ECHO Command: logoff %quserSessId%
ECHO.

:: Session Clean up
SET tstQUser=
SET tstQWin=
SET quserSessId=
SET quserSessId=
PAUSE


 
 
 

Notes

 

QUser

Qwinsta

Online Syntax Highlighter | Xt 256