Get-QADUser alternate date format

This topic contains 3 replies, has 3 voices, and was last updated by  Andrew Shaffer 3 years, 10 months ago.

  • Author
  • #10692

    Andrew Shaffer

    So I'm trying to get an alternate date format for the following:

    get-qaduser -searchroot ‘domain/Amsterdam' -IncludedProperties lastlogontimestamp -SizeLimit 0 | where { $_.lastlogontimestamp -eq $null } | where { $_.whencreated -le $60 } | select-object samaccountname, name, lastlogontimestamp, whencreated | out-file "c:\temp\Inactive60 $(get-date -f yyyy-MM-dd).txt”

    Basically, it outputs lastlogontimestamp and I want it to output it as the number of days since the last logon. Any suggestions? I appreciate the help!

  • #10702

    Dave Wyatt

    You could use a calculated property, something like this:

    get-qaduser -searchroot 'domain/Amsterdam' -IncludedProperties lastlogontimestamp -SizeLimit 0 |
    where { $_.lastlogontimestamp -eq $null } |
    where { $_.whencreated -le $60 } |
    select-object samaccountname, name, @{Name = DaysSinceLastLogon; Expression = { ((Get-Date) - $_.LastLogonTimestamp).Days }}, whencreated |
    out-file "c:\temp\Inactive60 $(get-date -f yyyy-MM-dd).txt"

    On a side note, you're filtering for $_.LastLogonTimestamp -eq $null , which doesn't make much sense with the rest of the code. Did you mean to do -ne $null ? I haven't used the Quest AD cmdlets, but I assume that they have something like -Filter or -LdapFilter arguments; you'd have better performance by using that instead of Where-Object.

  • #10703

    Richard Siddaway

    Unless I'm missing something I don't see how the code works

    get-qaduser -searchroot ‘domain/Amsterdam’ -IncludedProperties lastlogontimestamp -SizeLimit 0

    will get the user accounts

    where { $_.lastlogontimestamp -eq $null }

    will only pass users that don't have a lastlogontimestamp set

    where { $_.whencreated -le $60 }

    I'm assuming $60 is holding a date 60 days in the past so you are picking accounts that were created more than 60 days ago

    If I understand what you are trying to do – you want a list of all users who have logged on and you want to see the number of days since they logged on

    Something like this should work for you

    Get-QADUser -IncludedProperties lastlogontimestamp | where {$_.lastlogontimestamp -ne $null} |
    select samaccountname, name, @{N='daysSinceLogon'; E={ ( (get-date) - $($_.lastlogontimestamp) ).days } }, whencreated

    You need to subtract the lastlogontimestamp from today to get a timespan – the Days property gives you how many days ago the logon occurred

  • #10711

    Andrew Shaffer

    Hi sorry for the confusion, this was just the second half of the script that cleaned up user accounts that had never logged on. Thanks for the help Richard though! That one seems to be working.

You must be logged in to reply to this topic.