Skip to content

Instantly share code, notes, and snippets.

@AlexDev404
Last active May 4, 2026 00:43
Show Gist options
  • Select an option

  • Save AlexDev404/3f6471ece5b763e0c7d0ebe10a2e12e1 to your computer and use it in GitHub Desktop.

Select an option

Save AlexDev404/3f6471ece5b763e0c7d0ebe10a2e12e1 to your computer and use it in GitHub Desktop.
Self Destruct a Windows Profile on next logon
$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