Forum Discussion
dewandtcutterbuckcom
Apr 05, 2023Copper Contributor
Export 365 Users last logon time using powershell
I have created a PowerShell command that is supposed to export every users last logon time that is greater than 1 day. But it continues to create a blank document. Below is the command. Get-Ma...
Apr 05, 2023
This script should do it for you 🙂
# Import Active Directory module
Import-Module ActiveDirectory
# Get all user accounts and their last logon timestamps
$users = Get-ADUser -Filter * -Properties LastLogonTimestamp
# Filter out users who have logged on within the last 24 hours
$users = $users | Where-Object {($_.LastLogonTimestamp -ne $null) -and ((Get-Date) - (Get-Date $_.LastLogonTimestamp)).TotalHours -gt 24}
# Convert timestamps to a human-readable format
$users = $users | Select-Object Name, @{Name="LastLogon"; Expression={(Get-Date $_.LastLogonTimestamp -Format "yyyy-MM-dd HH:mm:ss")}}
# Export results to a CSV file
$users | Export-Csv -Path LastLogon.csv -NoTypeInformation