Bison Infosolutions Knowledgebase
Protect your Lenovo Server
Contact WhatsApp

PowerShell Commands to Check Current Logged-In User and Active RDP Sessions in Windows Server

In Windows environments, especially on Windows Server systems, administrators frequently need to identify the currently logged-in user, active Remote Desktop (RDP) sessions, console sessions, and disconnected users. PowerShell and built-in Windows command-line utilities provide quick and reliable ways to retrieve this information.

This article explains multiple methods to check:

  • Current logged-in user
  • Domain user details
  • Active RDP sessions
  • Session IDs
  • Remote users connected to a server
  • Detailed user information through PowerShell

These commands are extremely useful for:

  • System administrators
  • IT support engineers
  • Remote Desktop Server management
  • Multi-user Windows Server environments
  • Troubleshooting permission issues
  • Auditing user activity


1. Check Current Logged-In User Using whoami

The simplest command to identify the current user is:

whoami

Example Output

SERVER01\Administrator

Explanation

This command displays:

  • Computer or domain name
  • Currently logged-in username

Use Cases

  • Verify administrative account
  • Confirm RDP login user
  • Troubleshooting access permissions
  • Script automation validation


2. Check Current Username Using Environment Variables

PowerShell can directly access Windows environment variables.

Display Username Only

$env:USERNAME

Example Output

Administrator


Display Computer Name

$env:COMPUTERNAME

Example Output

SERVER01


Display Full Computer and Username

"$env:COMPUTERNAME\$env:USERNAME"

Example Output

SERVER01\Administrator


3. Get Detailed Current User Information

Use .NET classes inside PowerShell:

[System.Security.Principal.WindowsIdentity]::GetCurrent().Name

Example Output

DOMAIN\AdminUser

Benefits

  • Works reliably in scripts
  • Provides exact Windows identity
  • Useful for enterprise environments


4. Check Active RDP Sessions Using query user

One of the most important commands for Windows Server administrators.

query user

or

quser


Example Output

 USERNAME              SESSIONNAME        ID  STATE   IDLE TIME  LOGON TIME
Administrator console 1 Active none 5/23/2026 9:00 AM
User1 rdp-tcp#5 2 Active 2:15 5/23/2026 10:30 AM
User2 rdp-tcp#6 3 Disc 1:05 5/23/2026 8:00 AM


Understanding Output

ColumnDescription
USERNAMELogged-in account
SESSIONNAMEConsole or RDP session
IDSession ID
STATEActive or Disconnected
IDLE TIMEUser inactivity duration
LOGON TIMELogin timestamp


5. Check Sessions Using qwinsta

Another built-in utility for terminal services.

qwinsta


Example Output

 SESSIONNAME       USERNAME                 ID  STATE   TYPE
services 0 Disc
console Administrator 1 Active
rdp-tcp#5 User1 2 Active
rdp-tcp#6 User2 3 Disc


6. Difference Between quser and qwinsta

CommandPurpose
quserShows user-focused session details
qwinstaShows terminal session details
query userSame as quser
query sessionSimilar to qwinsta


7. Check All Logged-In Users with PowerShell

Using WMI:

Get-WmiObject Win32_ComputerSystem | Select-Object UserName

Example Output

UserName
--------
DOMAIN\AdminUser


8. Get Interactive User Sessions Using CIM

Modern PowerShell method:

Get-CimInstance Win32_LogonSession

Advanced filtering can identify:

  • Interactive logins
  • Remote logins
  • Service accounts


9. Find RDP User Session ID

To identify specific RDP session IDs:

quser

Look under the ID column.

Example:

User1   rdp-tcp#5   2 Active

Here:

  • Session ID = 2

Useful for:

  • Logging off users
  • Shadow sessions
  • Session management


10. Log Off a Specific User Session

After identifying session ID:

logoff 2

This logs off session ID 2.


11. Monitor Multiple RDP Users on Windows Server

For Remote Desktop Servers:

query session

Useful For

  • Terminal servers
  • Multi-user application servers
  • Citrix environments
  • Shared Windows Server systems


12. Check User via CMD Instead of PowerShell

Command Prompt alternative:

echo %username%

or

whoami


13. Detect Current User in Batch Scripts

Example:

@echo off
echo Current User: %username%
pause


14. PowerShell Script to Display Current User and Sessions

Write-Host "Current User:"
whoami

Write-Host "`nActive Sessions:"
query user


15. Common Administrative Scenarios

Scenario 1 — Verify Admin Account

Before performing server changes:

whoami


Scenario 2 — Identify Connected RDP Users

quser


Scenario 3 — Disconnect Idle Users

Find disconnected sessions:

query user

Then:

logoff <SessionID>


16. Security and Audit Benefits

Monitoring logged-in users helps:

  • Detect unauthorized access
  • Audit RDP usage
  • Prevent abandoned sessions
  • Improve server security
  • Manage licensing usage


17. Troubleshooting Tips

Command Not Found

If quser or qwinsta fails:

  • Ensure Remote Desktop Services tools are installed
  • Run PowerShell as Administrator


Access Denied

Use elevated PowerShell:

Run as Administrator


18. Best Practices

  • Regularly monitor active RDP sessions
  • Log off inactive users
  • Use least privilege accounts
  • Audit administrator logins
  • Restrict unnecessary remote access


Conclusion

PowerShell and built-in Windows utilities make it easy to identify:

  • Current logged-in user
  • Active RDP sessions
  • Disconnected sessions
  • Terminal server users

Commands like:

  • whoami
  • quser
  • query user
  • qwinsta

are essential tools for every Windows administrator and IT engineer.

Understanding these commands improves:

  • Server administration
  • User management
  • Security monitoring
  • Remote troubleshooting

#PowerShell #WindowsServer #Windows11 #RDP #RemoteDesktop #SystemAdministrator #ITSupport #WindowsAdmin #PowerShellCommands #WindowsCommands #ServerManagement #WindowsServer2019 #WindowsServer2016 #whoami #quser #qwinsta #RemoteDesktopServices #RDS #CMD #WindowsPowerShell #UserSessions #SessionManagement #ActiveDirectory #ServerAdmin #ITEngineer #WindowsNetworking #TerminalServer #MicrosoftServer #WindowsSecurity #ServerMonitoring #RemoteAccess #PowerShellScripting #WindowsTools #TechSupport #ITInfrastructure #SysAdmin #WindowsCLI #ServerSecurity #UserManagement #LoginSessions #RemoteUsers #AdminTools #WindowsEnvironment #ComputerAdministration #PowerShellTutorial #WindowsTerminal #DesktopSupport #EnterpriseIT #WindowsUtilities #CommandLineTools


PowerShell Windows Server whoami quser qwinsta query user query session RDP session Remote Desktop logged in user current user active session disconnected session Windows administration IT support system administrator Windows Server 2019
Sponsored