Format LastLogonDate

This topic contains 5 replies, has 2 voices, and was last updated by Profile photo of Istvan Szarka Istvan Szarka 2 years, 5 months ago.

  • Author
    Posts
  • #16772
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Hello,

    I'm trying to get a list from AD of computers that have not logged to the network in the last 60 days. I have the command worked and just need a bit of help with the date format. What I currently have is:

    $LLDate = (Get-Date).AddDays(-60)
    Get-ADComputer -Filter * -Properties Name, LastLogonDate, DistinguishedName |
     Where { $_.LastLogonDate -lt $LLDate } | Sort LastLogonDate -Descending |
     FT Name, LastLogonDate, DistinguishedName -AutoSize

    This displays [blockquote]COLINS 31/07/2013 16:13:26 CN=COLINS,CN=Computers,DC=domain,DC=local
    IT-TESTPC 25/07/2013 00:54:12 CN=IT-TESTPC,CN=Computers,DC=domain,DC=local
    WASIMA 15/07/2013 10:55:46 CN=WASIMA,CN=Computers,DC=domain,DC=local[/blockquote]

    Can I get the LastLogonDate to just display as dd/mm/yyyy format?

    Thanks
    Tony

  • #16773
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    Hello Tony,

    Sure, try this:

    $LLDate = (Get-Date).AddDays(-60)
    Get-ADComputer -Filter * -Properties Name, LastLogonDate, DistinguishedName |
     Where { $_.LastLogonDate -lt $LLDate } | Sort LastLogonDate -Descending |
     FT Name, @{name="LastLogonDate";expression={($_.LastLogonDate).ToShortDateString()}}, DistinguishedName -AutoSize
    

    The ToShortDateString() method displays only the day, month and year.

  • #16774
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Thanks

  • #16775
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    I've realized, I've updated my reply.
    I think you can solve it with a calculated property by using the ToShortDateString() method on the LastLogonDate property.
    However, I don't have access to an AD environment right now, so I can't test it, but it should work.

  • #16776
    Profile photo of Tony Wainwright
    Tony Wainwright
    Participant

    Works perfectly

    Thanks

  • #16777
    Profile photo of Istvan Szarka
    Istvan Szarka
    Participant

    Gladly!

You must be logged in to reply to this topic.