Public/Find-ActiveRDPSessions.ps1

function Find-ActiveRDPSessions
{
    <#
        .SYNOPSIS
        List all active RDP sessions on servers
     
        .DESCRIPTION
        List all active RDP sessions on servers (requires domain admin privileges)
 
        .NOTES
        Version : 1.0
        Author : Marc Bouchard
        e-Mail : marc@subnet192.com
        Twitter : @SUBnet192
     
        Requires ActiveDirectory module (run from computer with RSAT installed)
     
        .EXAMPLE
        Find-ActiveRDPSessions
    #>
    
    
    # Get today's date for the report
    $today = Get-Date
    
    # 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
        
        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))
            {
                Write-Host "$ServerName logged in by $RDPUser on $sessionType"
            }
        }
    }
} #END function