Hello everyone,
It has been an hour since I started to work on a new script in Active Directory. It is desired to query user LastLogin among all domain controllers in a domain. In a nutshell it will be used for:
Query LastLogin of ALL user accounts from ALL domain controllers and compare them to determine the best result.
I know it might takes quite long time for this query to process but since I am not a third-party person, so far I have written this script block:
$DC = Get-ADGroupMember "domain controllers"
$Allusers = get-aduser -Filter * -searchbase "ou=AllUsers,dc=Contoso,dc=Net" -Properties Samaccountname
$i = 0
$j = 0
#Main Part Of The Script#
foreach ($User in $AllUsers)
{
$LastLogin = $null
$UserArray[$i] = $User.Samaccountname
foreach ($DC in $AllDC)
{
$UN = Get-ADUser $User -Properties lastlogin -Server $DC
if ([DateTime]::FromFileTime($UN.LastLogin) -ge $LastLogin)
{
$LastLogin = $UN.LastLogin
$LoginArray[$j] = $LastLogin
}
}
$i += 1
$j += 1
}Well the questions are:
- Is it Optimized?
- Since I used two different arrays for this purpose, please kindly suggest me ideas on how to output this two arrays next to each other? ( I am not so professional in Powershell and Arrays especially) :)
Regards.
Mahdi Tehrani Loves Powershell 


Please kindly click on Propose As Answer or to mark this post as
and helpfull to other poeple.