top of page
Writer's pictureRattandeep singh

Make It Readable: TimeStamps in Activedirectory!


Hi I'm back with the timestamps again.

Today, I want to share a simple tricks that can help to get attributes available #timestamp converted to a readable format.


What are the attributes we are talking about here?

  • lastLogon

  • lastLogonTimestamp

  • pwdLastSet

First lets see the value of the 3 attributes using a #PowerShell command.

Get-ADUser TestUser1 -Properties lastLogon,lastLogonTimestamp,pwdLastset
















In the above command "Get-Aduser", I have user a parameter "Properties" to specify the attributes I am looking for a user in #ActiveDirectory. Becuase, by default you will not be able to get values of these attributes.


Now it's time to go back in my previous blog and check the conversion command. I used the FromFileTime method to convert the timestamp into a readable format. Lets do the same again.

[DateTime]::FromFileTime(133340964634191598)

In the above command I tried to convert the value "133340964634191598" of the attribute "lastLogon". In results I got the Readable format value as "Monday, July 17, 2023 7:41:03 PM ".








Now let's see how can we use this in a command to get the output directly.

Get-ADUser "testuser1" -Properties lastLogon,lastLogonTimestamp,pwdLastSet | select Samaccountname,`

@{name = "lastLogon"; expression = {[datetime]::FromFileTime($_.lastLogon)}},`

@{name = "lastLogonTimestamp"; expression = {[datetime]::FromFileTime($_.lastLogonTimestamp)}},`

@{name = "pwdLastSet"; expression = {[datetime]::FromFileTime($_.pwdLastSet)}} | FL

The above 4 lines are a sigle command and you can type the same in sigle lin in a console after removing tilda "`".













Now the results are there in the readable format. However, what if you need to customise the date format?

It's also an easy task. It can be done by changing the time into a string and defining a format. This time I will only keep one attribute to keep the command short.


Get-ADUser "testuser1" -Properties lastLogon | select Samaccountname,@{name = "lastLogon"; expression = {[datetime]::FromFileTime($_.lastLogon).ToString("dd-MM-yyyy::hh:mm:ss")}} | FL










Now the date is different that the default format. It has "-" instead of a "/". Time is separated from the date using "::" instead of a space and the time is in the 24 hours format.


TimeStamps seems difficult. However, with the conversion fundamentals it becomes very easy.

113 views0 comments

Comments


bottom of page