Home |

Trigat

Check for logged in users - AD

10-26-2020

This script is helpful when an Active Directory user keeps getting their account locked after their password has been reset. This can be caused by their account remaining logged into a computer.

You can also check if a Domain Admin forgot to log out of a server.

Language or Platform: Powershell

Code:

# This script checks if a user is logged into a list of computers

# Get list of all computers in Active Directory
# Get-ADComputer -Filter * -Property * | Select-Object Name | Out-File AllComputers.txt

$computers = gc .\AllComputers.txt

$Username = "username00"

ForEach ($comp in $computers) {
    if (Test-Connection -ComputerName $comp -BufferSize 16 -Count 1 -EA 0 -Quiet) {

		$proc = gwmi win32_process -computer $comp -Filter "Name = 'explorer.exe'"
		#Search collection of processes for username
		ForEach ($p in $proc) {
	    	$temp = ($p.GetOwner()).User
	  		if ($temp -eq $Username){
			    write-host "$Username is logged on $comp" -ForegroundColor Green
			    Invoke-Command -ScriptBlock { quser } -ComputerName $comp
		    }
        }
    }
  #   else {
  #  "$comp is Offline."
  #  }
}

# Command to kick user off computer
# LOGOFF UserID /server:actualcomputername

Back