$env:username is the currently logged-on user. The FileSystemWatcher has nothing to do with it. $UserList isn’t a magical, automatically populated variable - it’s empty in your case, because you’ve not put anything into it. I’m not seeing any cases where FileSystemWatcher provides user information.
Once you’ve configured it you can access the data via the Windows Security event log. However, it would be best to forward this data to a central logging solution which can analyse these events on the fly.
If i enable NTFS file system auditing in windows does it shows the username who deleted the files in the log file i have created in powershell script ?
or does it creates any other log file.
i have a requirement to show the users in the log file i have created in powershell script
Could you please guide me what i need to do for the above
The user information is being logged into the Windows Security event log once you enable NTFS file system auditing. Your PowerShell script will need to query the event log instead of using the File System Watcher.
NTFS file system auditing can be configured very granular to only log events for certain users, groups and actions. Play around with it and see what you get.