Skip to content

Instantly share code, notes, and snippets.

@davidzou2131
Created February 27, 2016 23:10
Show Gist options
  • Save davidzou2131/6b2252384dec6d75d72c to your computer and use it in GitHub Desktop.
Save davidzou2131/6b2252384dec6d75d72c to your computer and use it in GitHub Desktop.
Domain User Account Deleter

Domain User Account Deleter

About

This tool lists all domain user accounts on the current computer and allows to user to check and delete selected accounts. This will remove the entry from Windows' Profile List and also deletes their profile folder.

What Is Deleted

  • Registry
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\{SID}
  • Directories
    • C:\Users\{USERNAME} (Typically)

What Is Not Deleted

  • Registry
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileGuid\{GUID}
    • HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer\UserData\{SID}

Notes

  • Requires Admin. If UAC is enabled, you must "Run as Admin"
  • Current profile count is limited to 9999. It can be increased.
  • Domain profiles are detected if:
    • The SID starts with S-1-5-21-
    • A Guid value exist under the user's registry profile.
  • If an account profile failed to delete (files in use), it will inform you to manually delete it after a system restart.

Screenshot

Domain User Profile Deleter

#include <ButtonConstants.au3>
#include <GUIConstantsEx.au3>
#include <GUIListBox.au3>
#include <WindowsConstants.au3>
#include <GuiListView.au3>
#include <ListviewConstants.au3>
#include <Array.au3>
#RequireAdmin
Const $REG_KEY_ProfileList = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList"
Dim $DomainProfileCount = 0
$FRM_Main = GUICreate("Domain User Profile Deleter", 600, 435, -1, -1)
$LV_user_accounts = GUICtrlCreateListView("SID|Profile Path", 0, 0, 600, 383, $LVS_NOSORTHEADER, _
BitOR($LVS_EX_CHECKBOXES, $LVS_EX_HEADERDRAGDROP, $LVS_EX_FULLROWSELECT))
For $i = 1 To 9999
$sid = RegEnumKey($REG_KEY_ProfileList, $i)
If @error Then ExitLoop
; Account is a domain account if the value 'Guid' exists.
If StringLeft($sid, 9) = "S-1-5-21-" And RegRead($REG_KEY_ProfileList & "\" & $sid, "Guid") <> "" Then
GUICtrlCreateListViewItem($sid & "|" & RegRead($REG_KEY_ProfileList & "\" & $sid, "ProfileImagePath"), $LV_user_accounts)
$DomainProfileCount += 1
EndIf
Next
$BTN_Delete = GUICtrlCreateButton("Delete Selected", 465, 400, 125, 25)
; Autosize the columns
If $DomainProfileCount > 0 Then
_GUICtrlListView_SetColumnWidth($LV_user_accounts, 0, $LVSCW_AUTOSIZE)
_GUICtrlListView_SetColumnWidth($LV_user_accounts, 1, $LVSCW_AUTOSIZE)
EndIf
GUISetState(@SW_SHOW)
While 1
$nMsg = GUIGetMsg()
Switch $nMsg
Case $GUI_EVENT_CLOSE
Exit
Case $Btn_Delete
$todelete = False
$require_restart = False
$delete_prompt = "Are you sure you want to delete the following profiles?"
$failed_delete_prompt = "The following profiles were not successfully deleted." & @CRLF & "Please delete them manually after a system restart:"
; Generate confirmation prompt message (list of paths)
For $i = 0 to $DomainProfileCount
If _GUICtrlListView_GetItemChecked($LV_user_accounts, $i) Then
$todelete = True
$delete = StringSplit(_GUICtrlListView_GetItemTextString($LV_user_accounts, $i), "|")
$delete_prompt &= @CRLF & $delete[2]
EndIf
Next
If $todelete Then
; Confirmation prompt
$msgbox_response = MsgBox(BitOR($MB_ICONQUESTION, $MB_YESNO), "Delete Accounts", $delete_prompt)
If $msgbox_response = $IDYES Then
; Start deleting selected profiles
For $i = 0 to $DomainProfileCount
If _GUICtrlListView_GetItemChecked($LV_user_accounts, $i) Then
$delete = StringSplit(_GUICtrlListView_GetItemTextString($LV_user_accounts, $i), "|")
; Delete registry entry
RegDelete($REG_KEY_ProfileList & "\" & $delete[1])
; Delete profile folder
RunWait(@ComSpec & " /c " & "rmdir" & ' "' & $delete[2] & '"' & ' /s /q')
; Check if successfully deleted
If FileExists($delete[2]) Then
$require_restart = True
$failed_delete_prompt &= @CRLF & $delete[2]
EndIf
EndIf
Next
If $require_restart Then
MsgBox($MB_ICONWARNING, "Delete Accounts", $failed_delete_prompt)
Else
MsgBox($MB_ICONINFORMATION, "Delete Accounts", "The selected profiles have been deleted successfully.")
EndIf
Exit(0)
EndIf
Else
MsgBox($MB_ICONINFORMATION, "Delete Accounts", "No accounts selected. Nothing Deleted.")
EndIf
EndSwitch
WEnd
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment