Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Newbie PS question:

How to use PowerShell to get the last login date for a group of users?

This is probably me just being dense, but I've tried this a number of different ways and still can't get the results.

I have a function that will return the last logon date/time for a user

PS C:\> Get-ADUserLastLogon -username Bob
Bob last logged on at: 4/1/2015 7:45:34 AM

I can get the list of SharePoint related users with the following get-aduser command:
PowerShell
get-aduser -filter 'name -like "SP*" -or name -like "SharePoint*"' | get-aduser -properties * | select samaccountname


How can I pipe the results from the user list in to the last logon command to get the full last logon result list for the SharePoint users?
Posted

1 solution

Something like this should work:
PowerShell
Get-ADUser -filter 'name -like "SP*" -or name -like "SharePoint*"' -Properties samAccountName,lastLogonDate


EDIT: lastLogonDate, not lastLogon! The former returns a DateTime, whereas the latter returns a FILETIME[^].
 
Share this answer
 
v2
Comments
littleGreenDude 1-Apr-15 15:29pm    
Thank you. You are so close, I'm accepting your solution. This is a case where I was making things more complicated then they need to be. lastLogon returns a column with a really large number, I'm assuming this is number of seconds since some date/time. Using lastLogonDate will provide the results in a date/time format. Again, thank you very much for the help.

Get-ADUser -filter 'name -like "SP*" -or name -like "SharePoint*"' -Properties samAccountName,lastLogonDate

Another useful tidbit for those who stumble across this... know the extended properties:

http://social.technet.microsoft.com/wiki/contents/articles/12037.active-directory-get-aduser-default-and-extended-properties.aspx
Richard Deeming 1-Apr-15 15:32pm    
Sorry, I was looking at this script[^], and totally missed the DateTime::FromFileTime($time) part!

Good to hear it's working. I'll update the answer with your corrected code, in case it helps anyone else.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900