Home » PowerShell » Get-ADComputer Last Logon using PowerShell

Get-ADComputer Last Logon using PowerShell

The Get-AdComputer cmdlet in PowerShell is used to retrieve information about Active Directory computers. The Get-AdComputer command has a LastLogon attribute, which stores the date and time of the computer’s last successful logon to a domain controller.

In a large organization, the System administrator has to continuously monitor inactive or stale objects in the Active Directory. The Get-ADComputer last logon date-time helps to understand when was the last time the computer used.

In this article, we will discuss how to use Get-ADComputer to find the last logon date for the computer in the active directory and the computer’s last logon date and time in OU using PowerShell.

Get-ADComputer Last Logon in Active Directory

To get the last logon date for computers in the active directory and export the adcomputer last logon details to a CSV file, run the below command

 Get-ADComputer -Filter * -Properties * | Sort LastLogon | Select Name, LastLogonDate,@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}} | Export-Csv C:\adcomputers-last-logon.csv -NoTypeInformation

The Get-ADComputer cmdlet gets all the ad computers in the active directory using Filter * parameter.

The Properties * – gets all the properties of the adcomputer object and passes the output to the second command.

The second command uses the Sort command to sort computer objects by the last logon and pass output to the third command.

The third command selects a name, ad computer lastlogon, and lastlogondate.

LastLogon attribute is a Windows FileTime value, which is a 64-bit integer that represents the number of 100-nanosecond intervals since January 1, 1601 UTC. To convert a Windows FileTime value to a human-readable date and time, you can use the following PowerShell command.

@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}

The fourth command uses the Export-CSV cmdlet to export a list of adcomputer’s last logon in the CSV file.

The output of the above script in CSV file contains adcomputers last logon details as below

"Name","LastLogonDate","LastLogon"
"ENGG-PRO","7/29/2021 7:09:09 PM","7/31/2021 12:31:25 PM"

Cool Tip: How to get an active directory user’s last logon using PowerShell!

PowerShell Last Logon Computer in OU

To get the last logon date for the computer in OU, run the below command

Get-ADComputer -Filter * -SearchBase "OU=Servers,DC=SHELLPRO,DC=LOCAL" -Properties * | Sort LastLogon | Select Name, LastLogonDate,@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}} | Export-Csv C:\adcomputers-last-logon-ou.csv -NoTypeInformation

In the above PowerShell Get-ADComputer last logon script,

The Get-ADComputer cmdlet gets all the ad computers in the active directory using Filter *

SearchBase parameter search for ad computers in OU.

Properties * – gets all the properties of an ad computer object and passes the output to the second command.

The second command uses the Sort command to sort computer objects by the last logon computer in OU and pass output to the third command.

The third command selects a name, ad computer lastlogon, and lastlogondate. LastLogon property contains a value in number format that needs to be converted to date timestamp.

The below script helps the LastLogon property to convert to a date timestamp.

@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}

The fourth command uses the Export-CSV cmdlet to export a list of the last logon of the computer in OU in a CSV file.

Cool Tip: How to remove adcomputer PowerShell!

Conclusion

I hope the above article about how to find the Get-ADComputer last logon in the active directory and the last logon of computers in OU is helpful to you.

Use the LastLogon attribute to get an accurate last logon date time. Get-ADComputer has lastlogondate property as well but it’s not accurate and updated within 14 days.

You can find more topics about PowerShell Active Directory commands and PowerShell basics on the ShellGeek home page.