Thursday, February 14, 2013

Last User Login to Current Computer

Last User Login to Current Computer. Love this. Give me a list of domain users and their last login time to the current computer.

$data = @()
$NetLogs = Get-WmiObject Win32_NetworkLoginProfile
foreach ($NetLog in $NetLogs) {
if ($NetLog.LastLogon -match "(\d{14})") {
$row = "" | Select Name,LogonTime
$row.Name = $NetLog.Name
$row.LogonTime=[datetime]::ParseExact($matches[0], "yyyyMMddHHmmss", $null)
$data += $row
}
}

$data

Last Windows Domain logon time

Last Windows Logon Time
Love this.
This script uses the DirectorySearcher object to search for all users in Active directory. It then walks through the user accounts and determines the last logon date. 
The key point of the script is translating the [int64] number into something that can be read.

$searcher = New-Object DirectoryServices.DirectorySearcher([adsi]"")
$searcher.filter = "(objectclass=user)"
$users = $searcher.findall()
Foreach($user in $users)
{
if($user.properties.item("lastLogon") -ne 0)
{
$a = [datetime]::FromFileTime([int64]::Parse($user.properties.item("lastLogon")))
"$($user.properties.item(`"name`")) $a"
}

}

Got this from the Technet script center: