How to check what machines I’m logged into within a domain

active-directorymstscrdp

I often find myself RDP into multiple machines and have the connection time out, keeping me logged in. I then forget where I've been and my account stays logged in, blocking other users from accessing those machines.

Is there any way to query a user on a domain and list all the machines that user is logged into?

Best Answer

You can use PowerShell to find where your user is logged into. You will need the Active Directory cmdlets though:

# Import the Active Directory module for the Get-ADComputer CmdLet
Import-Module ActiveDirectory

# Query Active Directory for computers running a Server operating system
$Servers = Get-ADComputer -Filter {OperatingSystem -like "*server*"}

# Loop through the list to query each server for login sessions
ForEach ($Server in $Servers) {
    $ServerName = $Server.Name

    # When running interactively, uncomment the Write-Host line below to show which server is being queried
    # Write-Host "Querying $ServerName"

    # Run the qwinsta.exe and parse the output
    $queryResults = (qwinsta /server:$ServerName | foreach { (($_.trim() -replace "\s+",","))} | ConvertFrom-Csv) 

    # Pull the session information from each instance
    ForEach ($queryResult in $queryResults) {
        $RDPUser = $queryResult.USERNAME
        $sessionType = $queryResult.SESSIONNAME

        # We only want to display where a "person" is logged in. Otherwise unused sessions show up as USERNAME as a number
        If (($RDPUser -match "[a-z]") -and ($RDPUser -ne $NULL)) { 
            # When running interactively, uncomment the Write-Host line below to show the output to screen
            # Write-Host $ServerName logged in by $RDPUser on $sessionType
            $SessionList = $SessionList + "`n`n" + $ServerName + " logged in by " + $RDPUser + " on " + $sessionType
        }
    }
}


# When running interactively, uncomment the Write-Host line below to see the full list on screen
$SessionList

You will just need to tweak this for your situation. (i.e. computers and servers, not just servers)