Jump to content

Recommended Posts

Posted (edited)

Use command line to delete user profile.

DeleteProfile.au3 "UserName"

If you plan to use this winapi function, be very careful, if $sProfilePath is NULL, it will delete whole %SystemDrive%.

This behavior is very strange, because documentation states that if you pass NULL as ProfilePath, function will acquire ProfilePath from registry but this never occurs.

#NoTrayIcon
#include <Security.au3>

If $CmdLine[0] == 1 Then
   Local $UserName = $CmdLine[1]
Else
    Exit
EndIf

Local $aSID = _Security__LookupAccountName($UserName)

If $aSID[2] == 1 Then 
    Local $sProfilePath = RegRead("HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList\" & $aSID[0], "ProfileImagePath")
    
    Local $aProfilePath = StringSplit($sProfilePath, '\', 0)
    $sProfilePath = ""
    
    For $i=1 to $aProfilePath[0]
        Local $EnvVarTestStart = StringMid($aProfilePath[$i], 1, 1)
        Local $EnvVarTestEnd = StringMid($aProfilePath[$i], StringLen($aProfilePath[$i]), 1)
        If $EnvVarTestStart == "%" And $EnvVarTestEnd == "%" Then
            $aProfilePath[$i] = StringTrimLeft($aProfilePath[$i], 1)
            $aProfilePath[$i] = StringTrimRight($aProfilePath[$i], 1)
            $aProfilePath[$i] = EnvGet($aProfilePath[$i])
        EndIf
        $sProfilePath &= $aProfilePath[$i] & "\"
    Next  

    $sProfilePath = StringTrimRight($sProfilePath, 1)
    If StringLen($sProfilePath) <> 0 Or $sProfilePath <> EnvGet("AllUsersProfile") Or $sProfilePath <> EnvGet("AppData") Or $sProfilePath <> EnvGet("CommonProgramFiles") Or $sProfilePath <> EnvGet("ProgramFiles") Or $sProfilePath <> EnvGet("SystemRoot") Or $sProfilePath <> EnvGet("SystemDrive") Then
        Local $aRet = _DeleteProfile($aSID[0], $sProfilePath, @ComputerName)
    Else
        Exit
    EndIf  
Else
    Exit
EndIf

Exit

Func _DeleteProfile($sSID, $sProfilePath, $sComputerName)

    Local $aRet = DllCall("Userenv.dll", "int", "DeleteProfile", _
            "str", $sSID, "str", $sProfilePath, "str", $sComputerName)
      
    If $aRet[0] <> 0 Then
        Return True
    Else
        Return False
    EndIf
   
EndFunc    ;_DeleteProfile
Edited by Ghost1987

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...