Jump to content
Sign in to follow this  
Ghost1987

DeleteProfile

Recommended Posts

Ghost1987

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

Share this post


Link to post
Share on other sites

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
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.