How to get users login history in SharePoint OnPrem?

Is there any way to get users Login History in SharePoint on-prem 2016? Kindly Suggest.

SharePoint Asked on December 18, 2020

2 Answers

2 Answers

User below Powershell to get users from SharePoint.

This script will help save us developers a lot of time in getting all the users from an individual or group. So, here is the script.

  1. Copy the code below to a .ps1 file.
  2. Run the .ps1 file on the SharePoint PowerShell modules.
  3. You don't need to do any update on the script.

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue
$Currentime = get-date -format "yyyyMMdd_hhmmtt" $filename = "FarmUsers" $datafile = ("{0}{1}.csv" -f $filename, $Currentime)
$headerfile = "type,user,group,weburl,webtitle" $headerfile | out-file -FilePath $datafile
$iissitedata = get-spwebapplication foreach($farmsite in $iissitedata) {
foreach ($SiteCollection in $farmsite.sites)
write-host $SiteCollection -foregroundcolor Blue
foreach ($web in $SiteCollection.Allwebs)
write-host " " $web.url $ "users:" -foregroundcolor yellow
foreach ($usersite in $web.users)
write-host " " $usersite -foregroundcolor white
$data = ("RootUser,{0},-,{1},{2}" -f $usersite, $web.url,$
$data | out-file -FilePath $datafile -append
foreach ($group in $web.Groups)
Write-host " " $web.url $ -foregroundcolor green
foreach ($user in $group.users)
Write-host " " $user -foregroundcolor white
$data = ("GroupUser,{0},{1},{2},{3}" -f $user, $group, $web.url, $
$data | out-file -FilePath $datafile -append
} }

Get All Users In A SharePoint 2010/13/16 Farm Using PowerShell Script

Then try following code with loop by passing each user in below code to get last logon from AD.

$dcs = Get-ADDomainController -Filter {Name -like "*"}
$time = 0
foreach($dc in $dcs)
  $hostname = $dc.HostName
  $user = Get-ADUser $User.UserLogin.ToString().split('\')[1] | Get-ADObject -Properties lastLogon 
  if($user.LastLogon -gt $time) 
    $time = $user.LastLogon
$dt = [DateTime]::FromFileTime($time)
$UserData | Add-Member -type NoteProperty -name "last logged on at" -value $dt

Here $User is the SharePoint User which we found from SharePoint with domain name in it.

Answered by Hardik on December 18, 2020

Per my knowledge, there is no OOB solution to get users Login History in SharePoint 2016. However, you can develop a custom auditing solution. You'll need to add custom code to pages which involve login action(recording the login's in your page's code. U)

You can use cookie to be a flag for checking whether there is an user just log in. After the page loaded, if the cookie is null, it suggests that there is an user just log in our site, then we can get this user’s information using JavaScript Object Model and add the information into a custom list. With this list, we can monitor user’s login details. See the reply provided by Dennis from below article:

In addition, below post has a similar issue with you, which may be helpful to you:

Answered by imp. MSFT on December 18, 2020

Add your own answers!

Related Questions

Email a daily calendar summary to a Outlook Group

0  Asked on August 9, 2020 by david-boddie


SharePoint 2016 user profile service issue

2  Asked on August 6, 2020 by eric-vanroy


How to create a public facing website?

2  Asked on July 31, 2020 by keshav-taurah


Sharepoint designer workflow copy

2  Asked on July 30, 2020 by roxette


Ask a Question

Get help from others!

© 2022 All rights reserved.