0
0
PowershellHow-ToBeginner · 2 min read

PowerShell Script to Get User Last Login Date

Use the PowerShell command Get-ADUser -Identity username -Properties LastLogonDate | Select-Object Name, LastLogonDate to get a user's last login date from Active Directory.
📋

Examples

Inputusername: jsmith
OutputName LastLogonDate ---- ------------- jsmith 4/25/2024 10:15:30 AM
Inputusername: admin
OutputName LastLogonDate ---- ------------- admin 6/1/2024 8:00:00 AM
Inputusername: unknownuser
OutputGet-ADUser : Cannot find an object with identity: 'unknownuser' under: 'DC=domain,DC=com'.
🧠

How to Think About It

To find a user's last login, you query Active Directory for the user object and request the 'LastLogonDate' property. This property shows the most recent login time. You use the Get-ADUser cmdlet with the username and select the relevant property to display.
📐

Algorithm

1
Get the username input.
2
Use the Get-ADUser cmdlet to find the user in Active Directory.
3
Request the LastLogonDate property for that user.
4
Display the user's name and last login date.
5
Handle the case where the user does not exist.
💻

Code

powershell
Import-Module ActiveDirectory
$username = "jsmith"
$user = Get-ADUser -Identity $username -Properties LastLogonDate
if ($user) {
    Write-Output "User: $($user.Name)"
    Write-Output "Last Login: $($user.LastLogonDate)"
} else {
    Write-Output "User not found."
}
Output
User: jsmith Last Login: 4/25/2024 10:15:30 AM
🔍

Dry Run

Let's trace the script for username 'jsmith' through the code

1

Set username

$username = "jsmith"

2

Get user object

$user = Get-ADUser -Identity jsmith -Properties LastLogonDate $user.Name = "jsmith" $user.LastLogonDate = "4/25/2024 10:15:30 AM"

3

Check if user exists

$user is not null, proceed to output

4

Output results

Print "User: jsmith" and "Last Login: 4/25/2024 10:15:30 AM"

StepActionValue
1Set usernamejsmith
2Get user objectName=jsmith, LastLogonDate=4/25/2024 10:15:30 AM
3Check user existsTrue
4OutputUser: jsmith, Last Login: 4/25/2024 10:15:30 AM
💡

Why This Works

Step 1: Import Active Directory Module

The script starts by loading the Active Directory module with Import-Module ActiveDirectory to access AD cmdlets.

Step 2: Retrieve User Object

It uses Get-ADUser with the username and requests the LastLogonDate property to get the last login time.

Step 3: Check User Existence

The script checks if the user object was found to avoid errors when the username does not exist.

Step 4: Display Results

Finally, it prints the user's name and last login date to the console for easy reading.

🔄

Alternative Approaches

Using LastLogonTimestamp for approximate last login
powershell
Import-Module ActiveDirectory
$username = "jsmith"
$user = Get-ADUser -Identity $username -Properties LastLogonTimestamp
if ($user) {
    $lastLogon = [DateTime]::FromFileTime($user.LastLogonTimestamp)
    Write-Output "User: $($user.Name)"
    Write-Output "Approximate Last Login: $lastLogon"
} else {
    Write-Output "User not found."
}
LastLogonTimestamp replicates across domain controllers but may be up to 14 days old, so it's less precise but faster for large environments.
Querying all domain controllers for the most recent lastLogon
powershell
# This requires looping through all DCs and comparing lastLogon values, which is more complex and slower but accurate.
This method is more accurate but requires more code and time, suitable for critical audits.

Complexity: O(1) time, O(1) space

Time Complexity

The script queries a single user from Active Directory, which is a direct lookup and runs in constant time.

Space Complexity

The script stores only one user object and a few variables, so space usage is constant.

Which Approach is Fastest?

Using LastLogonDate with Get-ADUser is fastest for single users. Querying all domain controllers is slower but more accurate.

ApproachTimeSpaceBest For
Get-ADUser with LastLogonDateO(1)O(1)Quick single user lookup
Get-ADUser with LastLogonTimestampO(1)O(1)Approximate login for many users
Query all domain controllersO(n)O(n)Accurate audit across DCs
💡
Always run PowerShell as administrator and ensure you have permissions to query Active Directory.
⚠️
Beginners often forget to import the Active Directory module or run PowerShell with the right permissions.