Last active
May 4, 2026 00:43
-
-
Save AlexDev404/3f6471ece5b763e0c7d0ebe10a2e12e1 to your computer and use it in GitHub Desktop.
Revisions
-
AlexDev404 revised this gist
May 4, 2026 . 1 changed file with 8 additions and 14 deletions.There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -1,19 +1,13 @@ $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 -
AlexDev404 created this gist
May 4, 2026 .There are no files selected for viewing
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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,19 @@ # Define the username and profile path $username = $env:USERNAME $profilePath = "C:\Users\$username" # Create the command to remove the profile via CIM (most reliable) $scriptBlock = { $userSid = (New-Object System.Security.Principal.NTAccount($args[0])).Translate([System.Security.Principal.SecurityIdentifier]).Value Get-CimInstance -Class Win32_UserProfile | Where-Object { $_.SID -eq $userSid } | Remove-CimInstance -Confirm:$false } # Create a scheduled task to run at next logoff $taskName = "DeleteProfileTask" $action = New-ScheduledTaskAction -Execute "powershell.exe" -Argument "-NoProfile -Command &{$scriptBlock}" -ArgumentList $username $trigger = New-ScheduledTaskTrigger -AtLogOff Register-ScheduledTask -Action $action -Trigger $trigger -TaskName $taskName -User "SYSTEM" -Force # Self-destruct this script file Remove-Item -Path $MyInvocation.MyCommand.Path -Force Write-Host "Profile will be deleted on logout. Self-destruct initialized."