Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort-object lastlogon -descending?

I have to check the lastlogon for different users.

My script queries my domain controllers to output my report, however I have an issue.

My report does not come out in descending order. I added sort-object lastlogon -descending, but the dates don’t come out correctly in my file. Can you help me?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

$data = @()
$DCs = Get-ADDomainController -Filter * | Select-Object -ExpandProperty name
$users = 
@'
samaccountname;
user1
user2
'@ | ConvertFrom-Csv -Delimiter ';'
foreach ($DC in $DCs) {
            foreach($user in $users)
                {$data += Get-ADUser $User.samaccountname.Trim() -Properties displayname, userprincipalname, samaccountname, lastlogon -server $DC | Select-Object DisplayName, UserPrincipalName, SamAccountName, Enabled, @{name='LastLogon';expression={[datetime]::fromFileTime($_.lastLogon).ToString('yyyy-MM-dd')}} }
                    }
$data | Group-Object Lastlogon | Foreach-Object {$_.Group | Sort-Object lastLogon -Descending | Select-Object  -Last 10 | Export-Excel "C:\temp\lastlogon ($(Get-Date -Format "yyyy-MM-dd")).xlsx"}

write-host Done! -ForegroundColor Green

>Solution :

It’s unclear what you want to accomplish with your script but basically, if you .ToString(..) a DateTime object then Sort-Object will not know how to sort it correctly. Here is how you can approach your code:

$DCs = (Get-ADDomainController -Filter *).Name
$users = @'
samaccountname;
user1
user2
'@ | ConvertFrom-Csv -Delimiter ';'

& {
    foreach ($DC in $DCs) {
        foreach($user in $users) {
            $params = @{
                Properties = 'displayname', 'lastlogon'
                Server     = $DC
                Identity   = $User.samaccountname.Trim()
            }
            Get-ADUser @params | Select-Object @(
                'DisplayName'
                'UserPrincipalName'
                'SamAccountName'
                'Enabled'
                @{
                    Name = 'LastLogon'
                    Expression = {
                        [datetime]::fromFileTime($_.LastLogon)
                    }
                }
            )
        }
    }
} | Group-Object { $_.Lastlogon.ToString('yyyy-MM-dd') } | Foreach-Object {
    $_.Group | Sort-Object LastLogon -Descending | Select-Object -Last 10 |
    Export-Excel "C:\temp\lastlogon ($(Get-Date -Format "yyyy-MM-dd")).xlsx"
}

You also want to avoid adding elements (+=) to a fixed collection (@()).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading