Pagine

Scripting - Get-ADComputer - How to retrieve computer last logon date

There is an easy and quick way to get .TXT file with all AD computer names and relativse last logon dates.


  1. You must execute Powershell module with Administrative rights.
  2. Import-Module activedirectory
  3. Get-Help Get-ADComputer
  4. Get-ADComputer -Filter * -Properties *  | Sort LastLogonDate | FT Name, LastLogonDate -Autosize | Out-File C:\Temp\ComputerLastLogonDate.txt




Alternative approach could be:
  1. $DaysInactive = 90
  2. $time = (Get-Date).Adddays(-($DaysInactive))
  3. Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName
  4. Get-ADComputer -Filter {LastLogonTimeStamp -lt $time} -ResultPageSize 2000 -resultSetSize $null -Properties Name, OperatingSystem, SamAccountName, DistinguishedName | Export-CSV “C:\Temp\StaleComps.CSV” –NoTypeInformation
[original articles]