List Workstations and Last Logon Time from AD using Powershell

This will list computers and Last Logon Times from AD:
import-module ActiveDirectory
$dcs = Get-ADComputer -Filter { OperatingSystem -NotLike ‘*Server*’ } `
    -Properties OperatingSystem
foreach($dc in $dcs) { `
    Get-ADComputer $dc.Name -Properties lastlogontimestamp | `
    Select-Object @{n=”Computer”;e={$_.Name}}, @{Name=”Lastlogon”; `
    Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}
}

References: http://stackoverflow.com/questions/16965355/get-last-logon-time-computer-and-username-together-with-powershell
http://blogs.technet.com/b/askds/archive/2010/02/04/inventorying-computers-with-ad-powershell.aspx

0 thoughts on “List Workstations and Last Logon Time from AD using Powershell

  1. AnonIT

    Hi Jon
    I'm not sure why that would be the case. The query isn't optimized, so if you had lots of objects in AD, it may take some time to populate them all. You can separate the two parts into single lines and use measure-command to time how long it takes to run them. You will need to import-module activedirectory first:

    measure-command {$dcs = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem}

    measure-command {foreach($dc in $dcs) {Get-ADComputer $dc.Name -Properties lastlogontimestamp | Select-Object @{n="Computer";e={$_.Name}}, @{Name="Lastlogon"; Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}}}}

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *