Get Last Logged On User

From psadt | Evergreen Application Management
Jump to navigation Jump to search



Powershell function to get the last logged on user for a given machine or machines.

Multiple machines can be checked at once by including the hostnames in a text file. In the example below the file is stored at D:\clients.txt

TIP: Can be used with the Write Log function to write to a file.


#region Function
$ComputerNames = (Get-Content "D:\clients.txt")
ForEach ($ComputerName in $ComputerNames)
    Write-Host $ComputerName
    Get-WmiObject -Class win32_process -ComputerName $ComputerName | 
        Where-Object{ $_.Name -eq "explorer.exe" } | 
        ForEach-Object{ ($_.GetOwner()).Domain + "\" + ($_.GetOwner()).User; }