I created a new function that allows you to retrieve to LastLogin time on a local system. The script leverages the WinNT provider in combination with the ADSI type accelerator. The script is available for download in the TechNet Script Gallery: Get-LocalLastLogonTime.
A simple example of how to use this function is as follows:
.EXAMPLE
Get-LocalLastLogonTime -ComputerName localhost -UserName user1,JaapBrasser,administrator
Description
-----------
Will check the system for the LastLogin properties of user1, JaapBrasser and the administrator account.
The task can also be scheduled, the following code shows how to run this script as a scheduled task:
.EXAMPLE
PowerShell.exe -Command "& {. C:\Scripts\Get-LocalLastLogonTime.ps1; Get-LocalLastLogonTime -ComputerName server1,server2 -UserName JaapBrasser,administrator | Export-Csv C:\Log\Users.csv}"
Description
-----------
Will check server1 and server2 for the LastLogin time of JaapBrasser and administrator. This example is useful for scenarios when scheduling tasks or when executing this PowerShell script from batch files as it outputs the results into a csv file.
The complete function is available in the TechNet Script Library. To view this script or to participate in the discussions about this script either comment here or in the TechNet Script Gallery.
TechNet Script Gallery |
My entries in TechNet Script Gallery |
Get-LocalLastLogonTime |