Active Directory Get-ADComputer cmdlet gets one or more computers in the active directory. In a large organization, the System administrator has to continuously monitor inactive or stale objects in Active Directory. Get-ADComputer last logon date-time helps to understand when was the last time computer used.
In this article, I will explain how to use Get-ADComputer to find the last logon date for the computer in the active directory using PowerShell.
We will use PowerShell Get-ADComputer cmdlet to get computer object and active directory computer last logon and last logon computer in OU.
Quick to summarize about PowerShell Get-ADComputer syntax which we will be using in our command to get adcomputer last logon
Get-ADComputer [-AuthType <ADAuthType>] [-Credential <PSCredential>] -Filter <String> [-Properties <String[]>] [-ResultPageSize <Int32>] [-ResultSetSize <Int32>] [-SearchBase <String>] [-SearchScope <ADSearchScope>] [-Server <String>] [<CommonParameters>] Get-ADComputer [-AuthType <ADAuthType>] [-Credential <PSCredential>] [-Identity] <ADComputer> [-Partition <String>] [-Properties <String[]>] [-Server <String>] [<CommonParameters>] Get-ADComputer [-AuthType <ADAuthType>] [-Credential <PSCredential>] -LDAPFilter <String> [-Properties <String[]>] [-ResultPageSize <Int32>] [-ResultSetSize <Int32>] [-SearchBase <String>] [-SearchScope <ADSearchScope>] [-Server <String>] [<CommonParameters>]
Let’s understand with an example to Get-ADComputer last logon in active directory.
Get-ADComputer Last Logon in Active Directory
To get last logon date for computers in the active directory and export the adcomputer last logon details to 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
In the above PowerShell script,
Get-ADComputer cmdlet gets all the ad computers in active directory using Filter *
Properties * – gets all the properties of the adcomputer object and passes the output to the second command.
Second command sort computer object by last logon and pass output to the third command.
Third command select 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 date timestamp.
@{Name='LastLogon';Expression={[DateTime]::FromFileTime($_.LastLogon)}}
The fourth command uses the Export-CSV cmdlet to export a list of adcomputer 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: Read more to get active directory user last logon using PowerShell!
PowerShell Last Logon Computer in OU
To get 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,
Get-ADComputer cmdlet gets all the ad computers in 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.
Second command sort computer object by last logon computer in OU and pass output to the third command.
Third command select 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 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 computer in OU in CSV file.
Cool Tip: How to remove adcomputer PowerShell!
Conclusion
I hope the above article about how to find Get-ADComputer last logon in active directory and last logon of computers in OU is helpful to you.
Use the LastLogon property 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.