Powershell script to get all computers last logon time

PowerShell script that checks AD logs for last logon of all domain joined computers

Update

I’ve posted an updated version of this script in a new section of my blog. Because all the comments below relate to the original version of the script I’m going to leave that version posted here. You can find the new version of the script here.

I wrote a script to get the last time each computer logged into the domain.

This script is very similar to my script that gets user last logon times, which you can find here.

This script requires the Quest AD Cmdlets, which you can download here. (Check the box agreeing to the terms to see the download options.)

##============================================================================== ##==============================================================================

SCRIPT………: Get-AllComputerLastLogon

AUTHOR………: Clint McGuire

EMAIL……….:

VERSION……..: 1

DATE………..: 2011_05_26

LICENSE……..:

REQUIREMENTS…: Powershell v2.0, Quest AD Cmdlets

DESCRIPTION….: Gets all computer accounts’ last logon times and exports to CSV file.

NOTES……….:

CUSTOMIZE……:

##==============================================================================

REVISED BY…..:

EMAIL……….:

REVISION DATE..:

REVISION NOTES.:

## ##============================================================================== ##============================================================================== ##==============================================================================

START <code>

##============================================================================== $DCs = Get-QADComputer -ComputerRole DomainController $LastLogon = @{} ForEach ($DC in $DCs) { $Computers = Get-QADcomputer -Service $dc.dnshostname -ip lastlogontimestamp ForEach ($Computer in $Computers) { If ($Computer.lastlogontimestamp -ne $null) { $Time = $Computer.lastlogontimestamp | Get-Date -format u } Else { $Time = $Computer.lastlogontimestamp } $ComputerName = $Computer.ComputerName If ($LastLogon.ContainsKey($ComputerName)) { If ($LastLogon.Get_Item($ComputerName) -le $Time) { $LastLogon.Set_Item($ComputerName, $Time) } } Else { $LastLogon.Add($ComputerName, $Time) } } } $LastLogon.GetEnumerator() | Sort-Object Name |export-csv $home\ComputerLastLogon.csv -NoTypeInformation ##==============================================================================

END <code>

##==============================================================================

comments powered by Disqus