Get-LastLogon - Determine The Last LoggedOn User - Outputs Object

This function will list the last user logged on or logged in.  It will detect if the user is currently logged on via WMI or the Registry, depending on what version of Windows it runs against.

4.2 Star
33,724 times
Add to favorites
E-mail Twitter Digg Facebook
  • Powershell returns nothing
    1 Posts | Last post May 08, 2018
    • I was able to run the script and it worked. Now, it returns nothing by a single system name or  server.txt file.
      Any ideas?
  • Warning: Cannot index into a null array
    2 Posts | Last post September 26, 2017
    • I'm getting into powershell and I found this script to help us get the last logged in user and I'm getting this error when I run it: Warning: Cannot index into a null array. The script I am using is exactly what is posted except I changed the user profile location from 'Documents and Settings' to 'Users' and added in the line to allow it to function with Win10. 
      Any idea what could be going on? 
    • Skip this! I might have figured it out. I didn't add in the line to account for win10 on a recent refresh of the code. Thank you for writing this!
  • Inaccurate times and no Windows 10
    2 Posts | Last post June 27, 2017
    • Great script, but two issues I am having I'm hoping someone can help with. 
      1. No luck with Windows 10 machines.
      2. The majority of machines I ran this against shows the current time script was ran under the time column. I am running against several hundred computers and there's about 18 per minute the script gets too but stamps the time as the time script is run. Example:
      6/27/2017 8:34
      6/27/2017 8:34
      6/27/2017 8:34
      6/27/2017 8:35
      6/27/2017 8:35
      6/27/2017 8:35
      6/27/2017 8:35
      If I log in to one of the PCs there is no user logged in. Any ideas? 
    • Whoops; found the answer below to Windows 10 from Karl - see a few posts down. Still have the time stamp issue though. 
  • exclude certain users
    1 Posts | Last post December 07, 2016
    • Hi Brian,
      Thanks for this script, it's great. Only thing I would like to know is would it be possible to exclude certain users, to avoid getting myself (or other support users) from being shown as last logged in accounts. An example of code that could be used to accomplish this would be awesome as I'm not that good with PS scripting:)
  • if the machine is off
    1 Posts | Last post September 20, 2016
    • Hi Brian, 
      it is more interesting to obtain who logged in last to a certain domained computer. 
      Is this info available on domain controllers at all? Imagine a case when the machine of interest is currently off and you don't know its physical location. I searched the net and found that other people were also having an issue like that. Thanks!
  • getting last few users through this script
    1 Posts | Last post July 11, 2016
    • Hi Brian,
      Thanks for writing this script. I want to retrieve the last few more users  through this script. As this script will give the last logged on users, it can be a agent id (like backup agent or SCOM  agent) , due to this I want few more logged on users/apps id. please suggest ..\I am trying to modify  but its not working..
  • Getting all users last logon time
    3 Posts | Last post July 08, 2016
    • Hi Brian, While this script works great for getting the last logged on user.  Is there a way to get the times for all users and their last logon times with this script?
    • Not as it's currently written, but you could modify it to suit your needs.  It is a bit crude, but I'm either querying WMI or the NTUSER.DAT.Log file to determine last modified date.  You could potentially do this and return all results, not just the last logged in user.
    • Hi Brain,
      Thanks for writing this script , is there any way to list all SIDs as in my case lastloggged on user appears to be backupagent id or SCOM id. please help. if I can get a list of last 10 users name ..
  • Worked Once
    2 Posts | Last post March 29, 2016
    • After several attempts I got some computers to report output then realized I needed to submit to my requestor and results scrolled off screen so I tried to run again with output to file and now not getting any responding. All "RPC server is unavailable". Also tried to add the lines you suggested for output and that didn't work at all. Suggestions. 
    • I withdraw the question. Now that I look at the file, it is populated but the screen output was all "unavailable" Interesting....
  • Hi Brian, any chance the script will work on windows 10?
    3 Posts | Last post March 10, 2016
    • working like charm with Windows 7 and 8.1 but im not getting any results on windows 10 machines.
      thanks alot
    • Same for me, you are not alone. I didnt figure out yet, what the cause is.
    • For Windows 10 fix, add this to line 101
      [int]$build= [convert]::ToInt32($build, 10)
      The actual release jumps into the wrong os query (for XP), as $Build is not greater 6001, when not converted to integer. 
  • Export Output
    2 Posts | Last post February 25, 2016
    • Script is running perfectly. I want to export the output to csv file whcih contains following tabs computer,user,SID,time,Currently logged in.
      PS C:\> . .\Get-LastLogon.ps1
      $Servers = Get-Content "C:\ServerList.txt"
      Get-LastLogon -ComputerName $Servers
    • you can modify the current script like so:
      Go to line where it begins "Process {"
      create three new lines under it with following example:
      $Servers = Get-Content "C:\ServerList.txt"
      $ComputerName = $Servers
      $ComputerName = $ComputerName.Split(",")
      now we want to get the CSV on Desktop p.e.:
      Go to where it means: #Creating Custom PSObject For Output
      then there it is piping with Selected Objects.
      You just make some more pipes, it would be like so:
       | Select-Object Computer, User, SID, Time, CurrentlyLoggedOn | Format-Table -Property Computer, User, SID, Time, CurrentlyLoggedOn -AutoSize | Out-String | Out-File -Append C:\ClientInformations.csv
      *Format-Table & Out-String Pipe is optional, for me it looks better.
      Now you just have to do the following in PS:
      PS C:\> . .\Get-LastLogon.ps1
      Get-LastLogon -ComputerName $Servers
      Hope this helps! ;)
1 - 10 of 20 Items