AD Computer LastLogon Date format sorting

Welcome Forums General PowerShell Q&A AD Computer LastLogon Date format sorting

Viewing 8 reply threads
  • Author
    Posts
    • #214986
      Participant
      Topics: 5
      Replies: 15
      Points: 75
      Rank: Member

      Hello everyone,

      I am using below script to figure out the last logon date for computer objects in my AD:

       

      Get-ADComputer -Filter {Enabled -eq $true} -Properties LastLogon |

      Select-Object Name,@{n=‘LastLogon’;e={[DateTime]::FromFileTime($_.LastLogon)}}

      It is working fine however for some computer object it is giving output as MM\DD\YY and other are in DD\MM\YY format.

      How do I make sure that all the lastlogon output comes with same format as: DD\MM\YY format?

      Would really appreciate if anyone can help me here 🙂

       

      Thank you in advance.

       

    • #214989
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      If you want to force the format output of a property, you should use one of the format cmdlets like Format-List or Format-Table.  With those cmdlets you can create custom fields like you did in your Select-Object, but they will allow you to specify a format for the output.  For a list of all the format specifiers look here.  Here is a simplified example of how it would look.

      $result = [pscustomobject]@{Date=[datetime]"12/30/2020";name="Hello"}
      $result | Format-Table -Property name, @{n="Date";e={$_.date};f="dd/MM/yy"}
      
      

      For your script you should just be able to replace the Select-Object with either Format-Table or Format-List and include the appropriate specifier.

    • #215556
      Participant
      Topics: 5
      Replies: 15
      Points: 75
      Rank: Member

      Hello Mike,

      Appreciate your response. I think it is working as suggested. However, i would make sure that it only displays the Date not the time. How do i make edit/filter my code to reject/trim the Time from the output for all the server entry?

      Sorry but i am new to powershell and hence need your little help. Thanks.

    • #215571
      Participant
      Topics: 5
      Replies: 15
      Points: 75
      Rank: Member

      Hello Mike,

      I played around and got it working as per my need. Below is the code I am using:

      $Results = @(Get-ADComputer -Filter {Name -ne ‘0’}  -Properties LastLogon )|

      Select-Object Name, Enabled, @{n=‘LastLogon’;e={[DateTime]::FromFileTime($_.LastLogon).ToShortDateString()}}

      $Results | Export-Csv c:\temp\Lastlogontest.csv

      However now i ran into an issue when i import the report in Excel. I am seeing a different format in excel for 2 date types:

      13/02/2020 and 3/2/2020

      Excel sheet filter these 2 dates on different categories and hence i am having issues filtering data for a given month/date. If we can make 3/2/2020 to 03/02/2020 may help me filter the data in excel.

      Any idea how we can make sure all the date stamp comes in the same format so that excel doesnt confuses itself. ?

       

       

       

    • #215634
      Participant
      Topics: 4
      Replies: 2231
      Points: 5,414
      Helping Hand
      Rank: Community MVP

      Please, when you post code format it as code using the code tags “PRE”, Thanks. You may (re-)read the instruction you find in the very first post on top of the list of this forum: Read Me Before Posting! You’ll be Glad You Did!
      You should edit your existing posts. Do not create new ones.
      Thanks in advance.

    • #215844
      Participant
      Topics: 4
      Replies: 82
      Points: 251
      Helping Hand
      Rank: Contributor

      If you want to force a specific format you will need to use a format specifier in Format-Table then do you Export-CSV.  Have you tried that?

    • #215895
      Participant
      Topics: 12
      Replies: 1610
      Points: 2,508
      Helping Hand
      Rank: Community Hero

      Look at the documentation for string formats. Start with Standard formats and then if you want something specific then you can use Custom.

    • #216162
      Participant
      Topics: 3
      Replies: 324
      Points: 1,056
      Helping Hand
      Rank: Community Hero

      Here you go, just call the tostring() method with format specifier as detailed here and here.

      get-adcomputer -Filter {enabled -eq $true} -Properties lastlogon |
          Select-Object name,@{n='LastLogon';e={[datetime]::fromfiletime($_.lastlogon).tostring("dd/MM/yyyy")}}
      
      

      I hope this helps!

    • #217146
      Participant
      Topics: 5
      Replies: 15
      Points: 75
      Rank: Member

      Hello Doug,

      Thanks a lot for your time.

      It works absolutely fine and gives the data in the correct format which i was expecting.

      Thank you everyone for helping me with it 🙂

Viewing 8 reply threads
  • You must be logged in to reply to this topic.