How to use PowerShell command get-adcomputer to retrieve computer last logon date from AD

Retrieve computer last logon on Domain controller with PowerShell

Many times we need to know when a computer was active in AD environment. We have pushed some actions but the result doesn’t look to good because a lot of computer didn’t respond, applied, etc and are not mark as compliant.
Now let’s start, we need to run PowerShell as an administrator
1 If don’t have already installed  Active Directory Module for Windows PowerShell install by run Import-Module activedirectory

2 We will go to use Get-ADComputer, if you want to know more about what Get-ADComputer can do run Get-Help Get-ADComputer

3 Lets start to looking for a specific computer Test-VM to see what properties are available
Command: Get-ADComputer -Identity Test-VM
Result:
DistinguishedName : CN=TEST-VM,OU=Staging,OU=Workstations,DC=Domain,DC=local
DNSHostName       : Test-VM.Domain.local
Enabled           : True
Name              : TEST-VM
ObjectClass       : computer
ObjectGUID        : c02e8390-becc-4731-a6b0-7b4662e68203
SamAccountName    : TEST-VM$
SID               : S-1-5-21-3190024852-2712064565-171655375-28547
UserPrincipalName :
By default no information about last logged time stamp so let’s look to all properties

4 Get-ADComputer -Identity Test-VM -Properties *
This time the amount of information is higher and also cover our needs, showing the data for LastLogonDate
LastLogonDate           : 06/09/2019 11:56:16
If you run for one computer is fine but if you look for all AD than we want to format the table and looking only for a few properties like in our case the last logon so we will use -Autosize switch to the Format-Table Cmdlet
Get-ADComputer -identity Test-VM -Properties * | FT Name, LastLogonDate -Autosize
Now looks better 😊
Name    LastLogonDate
----    -------------
TEST-VM 06/09/2019 11:56:16

5 Until now we have focused one one computer Test-VM but what if we want to know Last Logon date for allcomputers?
Get-ADComputer -Filter * -Properties * | FT Name, LastLogonDate -Autosize
The computers with no LastLogonDate indicate that there is no LastLogon data (another ADComputer property), which is converted to LastLogonDate.
If we want to sort these in order using the last login properties we would use the following command
Get-ADComputer -Filter * -Properties *  | Sort LastLogonDate | FT Name, LastLogonDate -Autosize

6 We are happy with our results lets put all this data in a text file so we can disbaed some computers or delete them. We will use Out-File cmdlet option
Get-ADComputer -Filter * -Properties *  | Sort LastLogonDate | FT Name, LastLogonDate -Autosize | Out-File C:\Temp\ComputerLastLogonDate.txt

Or maybe you want more information like, OS version, IP address exported in excel / csv

Get-ADComputer -Filter * -Property * | Select-Object Name,LastLogonDate,OperatingSystemVersion,ipv4Address | Export-CSV ComputerLastLogonDate.csv -NoTypeInformation -Encoding UTF8

Wasn’t difficult, no :) ?