Get-LocalLastLogonTime - Get the LastLogin time on a local system

This script utilizes the WinNT provider to connect to either a local or remote system to establish if and when a user account last logged on that system. The output of the script is the computername, username, lastlogin and error messages if there are any.

 
 
 
 
 
3.7 Star
(3)
4,146 times
Add to favorites
11/15/2016
E-mail Twitter del.icio.us Digg Facebook
Sign in to ask a question


  • Run against computer list and for all user accounts?
    3 Posts | Last post December 16, 2016
    • Can you help me modify this to run it against multiple computers in a file and so that it provides the details on every local account on a computer? 
      
      I also want to then run it against a list of computers and for a specific list of user accounts in a text or csv file, is that possible?
      
      Thx!
      Dave
    • That is a good question, I do see the appeal of having the script be able to query all local user accounts on a system. I will update this script and add in a new parameter -AllUsers, I will let you know once I have implemented this.
      
      As for running this script against multiple computers, you could do something along these lines:
      
      . .\Get-LocalLastLogonTime.ps1
      Get-Content ComputerList.txt | ForEach-Object {
          Get-LocalLastLogonTime -ComputerName $_ -UserName JaapBrasser,administrator
      }
    • Just wondering, did the -AllUsers option get completed?
      
      Thanks.
  • Doesn't work
    2 Posts | Last post November 15, 2016
    • As has been mentioned, the script does not work, it just kicks out errors.
    • Hello Ebonweaver,
      
      I have updated the script to be compatible with older versions of PowerShell so the error messages should not appear anymore. Could you try with version 1.1?
      
      
      Regards,
      
      Jaap Brasser
  • Operator Error
    2 Posts | Last post November 15, 2016
    • Hi Jasper, thanks for sharing the script but it is giving me some operator error as:
      
      PS C:\PS> .\Get-LocalLastLogonTime -ComputerName localhost -UserName pruadmin
      The "=" operator is missing after a named argument.
      At C:\PS\Get-LocalLastLogonTime.ps1:48 char:23
      +             Mandatory, <<<< 
          + CategoryInfo          : ParserError: (:) [], ParseException
          + FullyQualifiedErrorId : MissingEqualsInNamedArgument
      
      
      Kindly have a look.
      
      Thanks
      Adi
    • Hello Adi,
      
      Indeed the 1.0 version of this script exhibited this behavior with older versions of PowerShell. I have updated this in version 1.1, could you verify that you no longer encounter this behavior?
      
      
      Regards,
      
      Jaap Brasser
  • Unable to display output
    4 Posts | Last post August 17, 2016
    • Hi there, firstly, thanks for contributing this script. When the script is run, it doesn't produce any output or errors. I'm using Powershell 4.0. Anything I may be doing wrong?
      
      PS F:\Apps\scripts\powershell> . .\Get-LocalLastLogonTime.ps1 -ComputerName localhost -UserName eugene
      PS F:\Apps\scripts\powershell>
      
      The account definitely exists on the computer. So not sure why there's no output. 
    • The problem is that the script I have uploaded contains a function. I write functions instead of script files as it is easy to load a function into memory of your session and then the function is available everywhere. So to run this script you should first load the script file, or dot source it as such:
      
      . .\Get-LocalLastLogonTime.ps1
      
      Now that you have the function "dot sourced", you can call this function as expected:
      
      Get-LocalLastLogonTime -ComputerName localhost -UserName eugene
      
      You will see that even if the user does not exist, output will still be generated. Hope that helps!
      
      
      Regards,
      
      Jaap Brasser
      
      
    • Yes it does help. Thanks again.
    • No problem Eugene, happy to help.