1

I have a script which gets the last logon times of each computer in the domain.

My script:

$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)}}
}

==================================

Result:

Computer Lastlogon
-------- ---------
DC1 6/06/2013 16:38:24
DC2 6/06/2013 16:30:40

=============================================

I also want to get who/which account made this logon. For example:

Computer Lastlogon User
-------- ------------------ ----
DC1 6/06/2013 16:38:24 user2
DC2 6/06/2013 16:30:40 user1

2

This one might not be perfect but it will get you on the right track. To get the exact last user, please see this script. It will give you further information on how to filter the exact last user. In the below example, I have used, select-object -First 1 which should be a pretty good indicator of the last logged on user. To get the last logged on user, you need to use

Get-WmiObject -Class Win32_UserProfile

To 'join' the Get-ADComputer and Get-WMIObject information, I have used a Hash Table.

If you are running this from a Domain Administrator account, you can take the -credential $credential part out. Otherwise, leave it in and you can run it from a normal workstation with the RSAT tools installed so Get-ADComputer is available.

Code below:

$computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*' } -Properties OperatingSystem
$credential = Get-Credential -Message "Please enter your administrator username and password"

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

    $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
    $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
    $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

    # Create hashtable for properties
    $properties = @{'Computer'=$pcinfo.Computer;
                    'LastLogon'=$pcinfo.Lastlogon;
                    'User'=$username.value
                   } #end $properties
    write-output (New-Object -Typename PSObject -Property $properties)
}

Check the formatting when you use it. Some of it I had to add in an escape (`) character to fit it into the script window.

Thanks, Tim.

0

This is Tim's Code and I updated it in order to display computer ip address & operating system, and export data.

 $computers = Get-ADComputer -Filter { OperatingSystem -NotLike '*Server*'} -Properties OperatingSystem

 foreach($computer in $computers) { 
     $pcinfo = Get-ADComputer $computer.Name -Properties ipv4Address, OperatingSystem ,lastlogontimestamp | 
               Select-Object @{n="Computer";e={$_.Name}}, 
              @{Name="Lastlogon";Expression={[DateTime]::FromFileTime($_.lastLogonTimestamp)}},ipv4Address,OperatingSystem

     $lastuserlogoninfo = Get-WmiObject -Class Win32_UserProfile -ComputerName $computer.name -Credential $credential | Select-Object -First 1
     $SecIdentifier = New-Object System.Security.Principal.SecurityIdentifier($lastuserlogoninfo.SID)
     $username = $SecIdentifier.Translate([System.Security.Principal.NTAccount])

     # Create hashtable for properties
     $properties = @{'Computer'=$pcinfo.Computer;
                     'LastLogon'=$pcinfo.Lastlogon;
                     'ipv4Address'=$pcinfo.ipv4Address;
                    'OperatingSystem'=$pcinfo.OperatingSystem
                     'User'=$username.value
                    } #end $properties
     write-output (New-Object -Typename PSObject -Property $properties) |
     export-csv .\Computers.csv -append -notypeinformation -encoding "unicode"         
            }

Powershell Get-ADComputer Properties Expression

  • Please check your code.  Are there supposed to be back-ticks in it?  Hint: use Code Sample formatting rather than Block Quote. – G-Man Nov 13 '17 at 13:41
  • Updated to code block – Frank Nov 13 '17 at 17:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.