AD Computer LastLogon Date format sorting

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 :slight_smile:

 

Thank you in advance.

 

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.

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.

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. ?

 

 

 

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.

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?

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

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!

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 :slight_smile:

[quote quote=216162][/quote]