← script library
PowerShell Script for Monitoring User Logon Events
Security & AuditingJanuary 12, 2025
In this post, we will create a PowerShell script that monitors user logon events on a Windows system. This script is useful for security professionals looking to track who is accessing their systems and when. It will utilize the Get-WinEvent cmdlet to fetch logon-related events from the Windows Event Log.
powershell
$logName = 'Security'
$eventId = 4624
$logonEvents = Get-WinEvent -LogName $logName -Id $eventId | Select-Object TimeCreated, Message
$logonEvents | ForEach-Object {
Write-Host "Logon Time: $($_.TimeCreated) - Event Message: $($_.Message)"
}
$userName = 'UserAccount' # Replace with the desired user account
$filteredEvents = $logonEvents | Where-Object { $_.Message -like "*$userName*" }
$filteredEvents | ForEach-Object {
Write-Host "Filtered Logon Time: $($_.TimeCreated) - Event Message: $($_.Message)"
}Run it across your fleet
This script runs as-is on a single host. Paste it into ServerEngine to schedule it, run it on a whole server group in parallel, and keep the credentials out of the file — see the scripts documentation and the credential store.
More in Security & Auditing
Enable the Windows Firewall with PowerShell
2026-08-17Check Windows Firewall Status with PowerShell
2026-08-17Disable SMBv1 with PowerShell
2026-08-17Enforce TLS 1.2 in the Registry with PowerShell
2026-08-17Enable RDP Network Level Authentication with PowerShell
2026-08-17Audit the Local Administrators Group with PowerShell
2026-08-17Ready when you are.
Try ServerEngine free for 7 days.