Skip to content

Instantly share code, notes, and snippets.

@spy86
Created October 28, 2019 21:16
Show Gist options
  • Select an option

  • Save spy86/069a73dae08b5017be3e30aa739564e5 to your computer and use it in GitHub Desktop.

Select an option

Save spy86/069a73dae08b5017be3e30aa739564e5 to your computer and use it in GitHub Desktop.
Powershell script to List User Last Logon
param(
$domain,
$users ) #end param
# Begin Functions
Function funWhatIf()
{
"what if: Perform operation obtain lastlogon time for user $user from
the $domain domain"
exit
} #end funWhatIf
function funGetAllUsers()
{
$attribute = "lastlogon"
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$domain")
$searcher.filter = "(&(objectClass=user)(objectCategory=person))"
$users = $searcher.findall()
foreach($user in $users)
{
if($user.properties.item("$attribute") -ne 0)
{
$attributeValue = [datetime]::FromFileTime([int64]::Parse($user.properties.item($attribute)))
"$($user.properties.item(`"name`")) $attributeValue"
} #end if user
} #end foreach user
} #end funGetAllUsers
function funGetUsers()
{
$attribute = "lastlogon"
$searcher = new-object DirectoryServices.DirectorySearcher([ADSI]"$domain")
ForEach($suser in $users)
{
$searcher.filter = "(&(objectClass=user)(sAMAccountName=$suser))"
$colusers = $searcher.findall()
foreach($user in $colUsers)
{
if($user.properties.item("$attribute") -ne 0)
{
$attributeValue = [datetime]::FromFileTime([int64]::Parse($user.properties.item($attribute)))
"$($user.properties.item(`"name`")) $attributeValue"
} #end if user
} #end foreach user
} #end foreach suser
} #end funGetUsers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment