sort-object -descending the lastlogondate

Below mentioned script fine. I am trying to sort-object -descending the lastlogondate but output not getting as expected, getting output in  generic   .

Can any one assist to sort the lastlogondate in to – descending.

Thanks in advance


$DCs = 'DC01','DC02'

$ADObjects = @{}
ForEach ($DC in $DCs) {
	Write-Host "Processing logons at $($DC) ..."
	Get-ADComputer -Filter * -Property lastLogon -SearchBase "DC=test,DC=com" -Server $DC | ForEach-Object {
		If ($ADObjects.ContainsKey($_.SamAccountName)) {
			If ($ADObjects[$_.SamAccountName].LastLogon -lt $_.lastLogon) {
				$ADObjects[$_.SamAccountName].LastLogon = $_.lastLogon
				$ADObjects[$_.SamAccountName].LastLogonAt = $DC
			}
		} Else {
			$ADObjects.Add($_.SamAccountName, ($_ | Select-Object SamAccountName, Name, LastLogon, @{Name='LastLogonAt'; Expression={$DC}}))
		}
	}
}
$ADObjects.Values |
	Sort-Object Name, SamAccountName, @{Name='Last Logon Date'; Expression={If ($_.lastLogon) {[datetime]::FromFileTime($_.lastLogon)} Else {'None'}}}, LastLogonAt |
	Export-CSV C:\temp\LastLogonReport.csv -NoTypeInformation

Open in new window

LVL 3
SAM ITAD windows Admin Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oBdACommented:
Just sort after the Select-Object, instead of replacing it with Sort-Object:
$DCs = 'DC01','DC02'

$ADObjects = @{}
ForEach ($DC in $DCs) {
	Write-Host "Processing logons at $($DC) ..."
	Get-ADComputer -Filter * -Property lastLogon -SearchBase "DC=test,DC=com" -Server $DC | ForEach-Object {
		If ($ADObjects.ContainsKey($_.SamAccountName)) {
			If ($ADObjects[$_.SamAccountName].LastLogon -lt $_.lastLogon) {
				$ADObjects[$_.SamAccountName].LastLogon = $_.lastLogon
				$ADObjects[$_.SamAccountName].LastLogonAt = $DC
			}
		} Else {
			$ADObjects.Add($_.SamAccountName, ($_ | Select-Object SamAccountName, Name, LastLogon, @{Name='LastLogonAt'; Expression={$DC}}))
		}
	}
}
$ADObjects.Values |
	Select-Object @{Name='Last Logon Date'; Expression={If ($_.lastLogon) {[datetime]::FromFileTime($_.lastLogon)} Else {'None'}}}, Name, SamAccountName, LastLogonAt |
	Sort-Object -Property 'Last Logon Date' -Descending |
	Export-CSV C:\temp\LastLogonReport.csv -NoTypeInformation

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SAM ITAD windows Admin  Author Commented:
thanks OBDA.

One more question : -

I have 05 Domain controllers , On each domain controller 1000 AD computer objects are there , I have executed the script and got the output aproximatly 5k AD computer objects.

Means for one AD computer object there will be an 5 entries on the output, Out of 05 entries for each AD computer I have to remove 4 entries and remain 01 entry that should be the latest one .

The resaon behind I have requested for assistance for -desending is to give a try to get the timestamp of object with the latest one & finding the soultion to remove the rest 04 old entries.

Any suguesstions please
0
SAM ITAD windows Admin  Author Commented:
any possible way that putting condition saying keep latest timestamp for each adcomputer object and remove rest of the entries or any other suggestions
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.