Last active
May 4, 2026 00:43
-
-
Save AlexDev404/3f6471ece5b763e0c7d0ebe10a2e12e1 to your computer and use it in GitHub Desktop.
Self Destruct a Windows Profile on next logon
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $username = $env:USERNAME | |
| # We embed the username directly into the string since -ArgumentList doesn't exist here | |
| $taskCommand = "Powershell.exe -NoProfile -WindowStyle Hidden -Command `"`$userSid = (New-Object System.Security.Principal.NTAccount('$username')).Translate([System.Security.Principal.SecurityIdentifier]).Value; Get-CimInstance Win32_UserProfile | Where-Object { `$_.SID -eq `$userSid } | Remove-CimInstance`"" | |
| $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument $taskCommand | |
| # Note: Since -AtLogOff isn't a native parameter, | |
| # this uses an Event Trigger for User Logoff (Event 4647) | |
| $trigger = New-ScheduledTaskTrigger -AtLogOn | |
| Register-ScheduledTask -Action $action -Trigger $trigger -TaskName "DeleteProfileTask" -User "SYSTEM" -Force | |
| Remove-Item -Path $MyInvocation.MyCommand.Path -Force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment