Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

How can I check if a specific user is logged in on a specific computer on my active directory network? I am administrator in this network and would prefer to do it with powershell but I am open to other options.

share|improve this question
    
Do you want to test for a specific user, a specific computer, a specific user on a specific computer or just anyone on any computer? –  John Gardeniers Jul 18 '11 at 10:26
    
Specific user in a specific computer. –  Artur Carvalho Jul 18 '11 at 10:40

3 Answers 3

up vote 3 down vote accepted

PSLoggedOn from the fine folks at SysInternals will do this.

share|improve this answer
    
Thank you mfinni, it works! –  Artur Carvalho Jul 18 '11 at 11:44

Here's a little function I whipped up to do this natively in powershell, without requiring PSLoggedOn.

It's very rough and could certainly use some polish, but it demonstrates the important information: use Get-WmiObject and query the Wim32_ComputerSystem class for the username property.

Unfortunately I think this is only accurate for locally logged on users -- you won't be able to see if a person is logged in over RDP with this method.

Here's the code:

function Test-LoggedOn {
    param(
        [parameter(Mandatory=$true,ValueFromPipeline=$true)][string]$username,
        [parameter(Mandatory=$true)][string]$computername
    )
    [regex]$pattern = "({\w}\\)?$($username.replace('\','\\'))"
    $wmi = Get-WmiObject -query "select username from win32_computersystem" -ComputerName $computername
    return $wmi.username -match $pattern
}
share|improve this answer

I have created a powershell function based on this link. It shows some text of the psloggedon command that I still don't know how to remove and the function has a loot of room for improvement... But it works, so HTH somebody else.

function UserLoggedOn{
    param($pc, $user)

    # creates an object with the fields Computer, Domain and User.  
    [object[]]$sessions = Invoke-Expression "d:\scripts\PsLoggedon.exe -x -l \\$pc" |
    Where-Object {$_ -match '^\s{2,}((?<domain>\w+)\\(?<user>\S+))|(?<user>\S+)'} |
    Select-Object @{ Name="Computer"
    Expression={"AGEXLUXS20"} 
    }, 
    @{ 
        Name='Domain' 
        Expression={$matches.Domain} 
    }, 
    @{ 
        Name='User' 
        Expression={$Matches.User} 
    }   

    $userfound = $false
    foreach($session in $sessions) {
        if ($session.User -eq $user) { $userfound = $true }
    }       

    if ($userfound) { Write-Host -ForegroundColor Green "$user is logged on \\$pc." }
    else  { Write-Host -ForegroundColor Red "$user is NOT logged on \\$pc." }

}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.