Use these commands to get logged users in local or remote server *Needs to be executed from a Server (it doesn't work from workstations)
Copy text below into a power shell session and execute to see the code in action.
# Misc - Server Admin - Get logged users in a Server # Core Snippet and Sample # www.PowerShellExamples.com # The best alternative is this windows command: quser # Unfortunately only work from PowerShell on servers # # *If you need to run from a workstation execute it from a DOS Command window using: # C:\>quser # C:\>quser /server:RemoteServerName # Sample from PowerShell: # Ensure you are on a server if((Get-CimInstance -ClassName Win32_OperatingSystem).ProductType -eq 1 ){ Write-Host "Error. This is a WorkStation. Remember this only works from a server!" -ForegroundColor Red }else{ Write-Host "Get logged users on this server $env:ComputerName" -ForegroundColor Magenta # Command to get logged users locally quser Write-Host "`r`n`r`nGet logged users on remote server RemoteServerName" -ForegroundColor Magenta # Replace [RemoteServerName] to get users logged on that server quser /server:RemoteServerName }
PS C:\> Get logged users on this server ThisServerName USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME User1 2 Disc 2+14:20 5/12/2022 9:30 AM >User2 rdp-tcp#63 3 Active . 5/23/2022 11:04 AM User3 4 Disc 17+12:11 5/23/2022 3:17 PM Get logged users on remote server RemoteServerName USERNAME SESSIONNAME ID STATE IDLE TIME LOGON TIME user1 2 Disc 17+12:11 5/23/2022 3:18 PM user2 3 Disc 17+16:54 5/23/2022 4:05 PM