PowerShell Script to Get Last logged-on time of AD users of different domain
Scenario:
You need to get Last logged-on time of AD users of different domain.
Below are the steps:
Step 1 : Find a DC of that domain:
Get-ADDomainController -DomainName -Discover -NextClosestSite
Above command outputs server name of given domain.
Step 2: Get-ADUser properties in that server
Get-ADUser -Server -Filter {Name -like " "} | Get-ADObject -Properties lastLogon
Possible Filters that can be used:
GivenName : First Name
Surname : Last name
Name :Login id
SamAccountName : Login id
UserPrincipalName : Login id@domain.com
My PowerShell script:
You need to get Last logged-on time of AD users of different domain.
Below are the steps:
Step 1 : Find a DC of that domain:
Get-ADDomainController -DomainName
Above command outputs server name of given domain.
Step 2: Get-ADUser properties in that server
Get-ADUser -Server
Possible Filters that can be used:
GivenName : First Name
Surname : Last name
Name :Login id
SamAccountName : Login id
UserPrincipalName : Login id@domain.com
My PowerShell script:
$GivenDomain = "mydomain.com"
$IDs = @(
"myadaccount1"
"myadaccount2"
"myadaccount3"
"myadaccount4"
)
$ErrorActionPreference = "silentlycontinue"
$dc = Get-ADDomainController -DomainName $GivenDomain -Discover -NextClosestSite
write-host Domain Controller : $dc.Name
foreach ($ID in $IDs) {
$User = Get-ADUser -Server $dc -Filter {Name -like $ID} | Get-ADObject -Properties *
$LLtime = $User.lastLogonTimestamp
$pwdLastSet = $User.pwdLastSet
$dtLLtime = [DateTime]::FromFileTime($LLtime)
$dtpwdLastSet = [DateTime]::FromFileTime($pwdLastSet)
Write-Host $ID "last logged on at :" $dtLLtime -ForegroundColor Green
Write-Host $ID "pwdLastSet at :" $dtpwdLastSet -ForegroundColor Yellow
""
""
}
Comments
Post a Comment