Support
 
Phone Get Quote
 
Support
 
US: +1 888 720 9500
US: +1 888 791 1189
Intl: +1 925 924 9500
Aus: +1 800 631 268
UK: 0800 028 6590
CN: +86 400 660 8680

Direct Inward Dialing: +1 408 916 9892

How to get current logged on user

The following is a comparison between the procedures of identifying currently logged on users with Windows PowerShell and ADAudit Plus:

PowerShell

Steps to obtain current logged on user using PowerShell:

  • Define the domain from which you want to retrieve the report.
  • Find the LDAP attributes you need to fetch the report.
  • Identify the primary DC to retrieve the report.
  • Compile the script.
  • Execute it in Windows PowerShell
  • The report will be exported in the given format.
  • To obtain the report in a different format, modify the script.

Sample Windows PowerShell script

Import-Module ActiveDirectory
 
[System.Security.Principal.WindowsIdentity]::GetCurrent().Name
{
$dcs = Get-ADDomainController -Filter {Name -like "*"}
$users = Get-ADUser -LDAPFilter "(&(objectCategory=person)(objectClass=user))"
$time = 0
$exportFilePath = "c:\scripts\lastLogon.csv"
$columns = "name,username,datetime"
Out-File -filepath $exportFilePath -force -InputObject $columns
foreach($user in $users)
{
foreach($dc in $dcs)
{ 
$hostname = $dc.HostName
$currentUser = Get-ADUser $user.SamAccountName | Get-ADObject -Server $hostname -Properties lastLogon
if($currentUser.LastLogon -gt $time) 
{
$time = $currentUser.LastLogon
}
}
if($time -eq 0){
$row = $user.Name+","+$user.SamAccountName+",Never"
}
else {
$dt = [DateTime]::FromFileTime($time)
$row = $user.Name+","+$user.SamAccountName+","+$dt
}
Out-File -filepath $exportFilePath -append -noclobber -InputObject $row
$time = 0
}
}
 Copied
Click to copy entire script

ADAudit Plus

To obtain the report,

  • Reports -> Local Logon-Logoff -> Currently Logged On Users
  • Select the required 'Domain'.
  • Select 'Export as' to export the report in any of the preferred formats (CSV, PDF, HTML, and XLS).

Screenshot

powershell-get-current-logged-on-user-1
 

Following are the limitations to obtain report of currently logged on users using native tools like Windows PowerShell:

  • We can run this script only from the computers which has Active Directory Domain Services role.
  • The script has to be modified, every time the report has to be exported in a different format.
  • Applying more filters will increase the LDAP query complexity

ADAudit Plus will automatically scan all DCs in the domain to retrieve information about the users currently logged on interactively to a computer, generate the report and present it in a simple and intuitively designed UI.

  • Avoid complex PowerShell-scripting, and simplify AD change auditing with ADAudit Plus.
  •  
  • By clicking 'Get Your Free Trial', you agree to processing of personal data according to the Privacy Policy.
  •  
  • Thanks!
  • Your download is in progress and it will be completed in just a few seconds! If you face any issues, download manually here.

Related Resources

ADAudit Plus Trusted By