PowerShell List Domain Users Logon Time

Copy script To Notepad

$NumDays = 0

$LogDir = “.\Users-Last-Logon.csv”

$currentDate = [System.DateTime]::Now

$currentDateUtc = $currentDate.ToUniversalTime()

$lltstamplimit = $currentDateUtc.AddDays(- $NumDays)

$lltIntLimit = $lltstampLimit.ToFileTime()

$adobjroot = [adsi]”

$objstalesearcher = New-Object System.DirectoryServices.DirectorySearcher($adobjroot)

$objstalesearcher.filter = “(&(objectCategory=person)(objectClass=user)(lastLogonTimeStamp<=” + $lltIntLimit + “))”

$users = $objstalesearcher.findall() | select `

@{e={$_.properties.cn};n=’Display Name’},`

@{e={$_.properties.samaccountname};n=’Username’},`

@{e={[datetime]::FromFileTimeUtc([int64]$_.properties.lastlogontimestamp[0])};n=’Last Logon’},`

@{e={[string]$adspath=$_.properties.adspath;$account=[ADSI]$adspath;$account.psbase.invokeget(‘AccountDisabled’)};n=’Account Is Disabled’}

$users | Export-CSV -NoType $LogDir

And Save The File As To All Files Logon.ps1

1 Comment:

Leave a Reply

Your email address will not be published.


CAPTCHA Image
Reload Image

This site uses Akismet to reduce spam. Learn how your comment data is processed.

WhatsApp Logo IT World