Hello,
I want to know which file or folder was deleted by whom.
The problem is that there is no file or folder name in ID 4660 and I need to extract the file or folder name from ID 4663, but how do I link these together? How do I know which ID 4660 is related to which ID 4663? What field is common between these IDs?
For example, A user named Jason creates a file or folder named Windows and either he or another user named James deletes this file or folder. I want to generate an output that tells me that the file or folder named Windows was deleted by the user named James.
For my purpose, I found the following script. What’s the problem?
# Get Event ID 4663 (Object Access)
$events4663 = Get-WinEvent -FilterHashtable @{LogName='Security'; ID=4663} | ForEach-Object {
[PSCustomObject]@{
TimeCreated = $_.TimeCreated
ObjectHandleID = $_.Properties[7].Value
ObjectName = $_.Properties[6].Value
User = $_.Properties[1].Value
}
}
# Get Event ID 4660 (Object Deletion)
$events4660 = Get-WinEvent -FilterHashtable @{LogName='Security'; ID=4660} | ForEach-Object {
[PSCustomObject]@{
TimeCreated = $_.TimeCreated
ObjectHandleID = $_.Properties[7].Value
User = $_.Properties[1].Value
}
}
# Correlate events
foreach ($event4660 in $events4660) {
$matching4663 = $events4663 | Where-Object { $_.ObjectHandleID -eq $event4660.ObjectHandleID }
if ($matching4663) {
Write-Output "File/Folder '$($matching4663.ObjectName)' was deleted by '$($event4660.User)' at $($event4660.TimeCreated)."
}
}
I expect output like the following:
File/Folder 'C:\New Folder' was deleted by 'Jason' at 2/24/2025 10:15:00 AM.
Gotcha. Well you are getting all the event’s that you need. It’s not correlating the events by object handle id correctly when running the events through the where-object pipeline.
Using the -match comparison you can obtain all matches and since there should only be a single match for the handle id in each of the event log streams this will also work.
# Correlate events
foreach ($event4660 in $events4660) {
$matching4663 = $events4663 -match $event4660.ObjectHandleID
if ($null -ne $matching4663[0]) {
Write-Host "File/Folder '$($matching4663[0].ObjectName)' was deleted by '$($event4660.User)' at $($event4660.TimeCreated)."
Write-host ""
}
}
What’s wrong with your original code? It looks like you may have copy pasted this code from ChatGPT. Which is fine with me, but you should learn how to step through your code and debug it before asking what’s wrong with it…
You are looking for an ID property in the properties property which doesn’t exist. You are comparing with a static value of 0x84, 0x86, 0x80.