6
votes

I'm looking for Powershell (preferred) script, or .CMD or .VBS, to delete a specific user profile on a workstation (WinXP) or terminal server (2000, '03 or '08). I know all about the delprof utility... That only allows you delete based on a period of inactivity. I want a script to:

  • prompt admin for a username
  • delete that username's profile
    • and to delete the entire profile - registry hive too (not just the folder structure within Documents and Settings).
    • The same way it would if you went to My Computer> Properties> Advanced tab> User Profiles Settings> and deleted profiles from there.

Any ideas? All I can think of is doing an AD lookup to get the SID of the user specified, then using that to delete the correct registry hive too... something simpler would be nice though...

Basically, my HelpDesk used to be local administrators on our Citrix servers and a common fix for various issues was for them to delete a user's profile on the citrix server(s) and have that user log back in - voila, whatever issue they had was resolved. Going forward, in new Citrix environment, they will no longer be local admins on those boxes, but still need to be able to delete profiles (deleting the entire profile: folder and reg hive is key). thanks.

8 Answers 8

1
vote

Powershell does it in pretty easy way if you are using windows 7 or windows 2008 computer.

http://techibee.com/powershell/powershell-script-to-delete-windows-user-profiles-on-windows-7windows-2008-r2/1556

0
2
votes

I wrote this VB script for a similar question on Server Fault. It will cycle through each Profile on the target machine, and prompt you (one by one) if you want to delete the profile. It does this the using WMI Win32_UserProfile, so it will be a clean removal.

It'll ask you for the FQDN of the target machine. If you are getting permissions errors, change the Username and Password to reflect an account that has Local Admin provs on the target machine.

Option Explicit
On Error Resume Next

Dim strComputer
Dim objWMIService
Dim propValue
Dim objItem
Dim SWBemlocator
Dim UserName
Dim Password
Dim colItems
Dim strMessage
Dim deleteResponse

strComputer = ""
UserName = ""
Password = ""
strMessage = ""

strComputer = InputBox("Please enter the FQDN of the new computer:")

If strComputer = "" Then
    WScript.quit
End If

If Not Ping (strComputer) Then
    MsgBox "The computer (" + strComputer + ") is not responding to ping - exiting"
    WScript.quit
End if

Set SWBemlocator = CreateObject("WbemScripting.SWbemLocator")
Set objWMIService = SWBemlocator.ConnectServer(strComputer,"root\CIMV2",UserName,Password)
Set colItems = objWMIService.ExecQuery("Select * from Win32_UserProfile",,48)
For Each objItem in colItems
    strMessage = ""
    If not objItem.LastDownloadTime = "" Then 
        strMessage = strMessage + "LastDownloadTime: " & left(objItem.LastDownloadTime,8) + Chr(10) + Chr(13)
    End If

    If Not objItem.LastUploadTime = "" Then
        strMessage = strMessage + "LastUploadTime: " & left(objItem.LastUploadTime,8) + Chr(10) + Chr(13)
    End if

    if not objItem.LastUseTime = "" then
        strMessage = strMessage + "LastUseTime: " & left(objItem.LastUseTime,8) + Chr(10) + Chr(13)
    End If

    If Not objItem.Loaded  = "" Then
        strMessage = strMessage + "Loaded: " & objItem.Loaded + Chr(10) + Chr(13)
    End If

    If not objItem.LocalPath = "" then
        strMessage = strMessage + "LocalPath: " & objItem.LocalPath + Chr(10) + Chr(13)
    End If

    if not objItem.RefCount = "" then
        strMessage = strMessage + "RefCount: " & objItem.RefCount + Chr(10) + Chr(13)
    End If

    if not objItem.RoamingConfigured = "" then
        strMessage = strMessage + "RoamingConfigured: " & objItem.RoamingConfigured + Chr(10) + Chr(13)
    End If

    if not objItem.RoamingPath = "" then
        strMessage = strMessage + "RoamingPath: " & objItem.RoamingPath + Chr(10) + Chr(13)
    End If

    if not objItem.RoamingPreference = "" then
        strMessage = strMessage + "RoamingPreference: " & objItem.RoamingPreference + Chr(10) + Chr(13)
    End If

    if not objItem.SID = "" then
        strMessage = strMessage + "SID: " & objItem.SID + Chr(10) + Chr(13)
    End If

    if not objItem.Special = "" then
        strMessage = strMessage + "Special: " & objItem.Special + Chr(10) + Chr(13)
    End If

    if not objItem.Status = "" then
        strMessage = strMessage + "Status: " & objItem.Status + Chr(10) + Chr(13)
    End If

    strMessage = strMessage + Chr(10) + Chr(13) + Chr(10) + Chr(13) + "Do you wish to delete this profile?"

    deleteResponse = MsgBox (strMessage,35,"Profile Found")

    Select Case deleteResponse
        Case 6
                Err.Clear
                objItem.Delete_
                If Err.Number = 0 Then 
                        MsgBox("Profile " & objitem.localpath & " on " & strComputer & " deleted")
                Else
                        MsgBox("Profile " & objitem.localpath & " on " & strComputer & " NOT deleted - Is user logged in?")             
                End If
    End Select

Next

Function Ping(strHost)

    dim objPing, objRetStatus

    set objPing = GetObject("winmgmts:{impersonationLevel=impersonate}").ExecQuery _
      ("select * from Win32_PingStatus where address = '" & strHost & "'")

    for each objRetStatus in objPing
        if IsNull(objRetStatus.StatusCode) or objRetStatus.StatusCode<>0 then
                Ping = False
        else
            Ping = True
        end if
    Next
End Function

It doesn't work using the same workflow you detailed (it requesting a username first). The problem with this approach is that the Win32_UserProfile doesn't contain the Username, only thr SID. When the user logs into the machine the SID is used to decide which profile is the correct one. This prevents problems with renaming a user accounts in AD.

1
  • Thanks that looks like something that can work with. Seems to need some updating though, I don't find the Win32_UserProfile class. I do have others like Win32_UserAccount and NetworkLoginProfile so when I mess around a bit that starts displaying SIDs in the prompt but then I'm in a loop, I'll mess with it. Anyone have a powershell version similar?
    – Jordan W.
    Oct 9, 2009 at 21:58
1
vote

tsprofilecleaner is a free gui that can remove a profile from a list of servers: link text

1
vote

I think the Cntrl+Alt+Del Consultancy in Australia developed a tool that might help you. It's called REMPROF and with this command, you can just delete a single profile off a server. Of course, you would have to substitute the actual user name in the command:

remprof USERNAME

I don't believe you can run this remotely from another server & you can't run this without administrative rights, unfortunately. I don't believe your support staff will be able to manage users & user profiles on the any server without being a member of the local admins group.

At any rate, you can download REMPROF & many other useful tools from the Cntrl+Alt+Del Consultancy web site here:

http://www.ctrl-alt-del.com.au/CAD_TSUtils.htm

I hope this helps.

Alicia

1
vote

Well instead we decided there was no good enough reason to keep any profiles on Citrix servers (they're roaming anyway with any real data redirected) so we set policy to delete profile when user logs off. thanks all for the suggestions.

0
votes

We use User Profile Hive Cleanup Service + small script which remove all USER profile when server start. Working goood

1
  • UPHClean does not delete profiles, it unmaps registry handles still in use during logoff. Jan 14, 2011 at 9:25
-1
votes

You can use delprof or remprof for profile deletion. You can create a script from those commands or you can use tsprofcleaner.

-2
votes

The users hive is located under their user directory (NTUSER.DAT) so deleting the directory with an rmdir /s /q {username} should suffice. The only part that wouldn't be deleted is under the profile list in the registry HKEY_LOCAL_MACHINE\Software\Microsoft\Windows NT\CurrentVersion\ProfileList but all that does is map the SID to a profile location.

If the user logs in and the directory doesn't exist, it'll be created again.

Unfortunately, without having them elevate themselves as admins, they won't be able to delete user profiles.

Batch file:

set /p USERTODEL=Enter the username to delete: 
set USERTODEL=C:\Users\%USERTODEL%
if exist "%USERTODEL%" rmdir /s /q %USERTODEL%
2
  • 3
    Your script won't work if the profile folder isn't the username. For exemple if a user with the same username but a different SID has already connect in the past, the profile will be stored in C:\Users\%domain%.%username% or something similar.
    – Benoit
    Oct 9, 2009 at 20:54
  • 2
    If you leave the registry key below ProfileList you will get a temporary profile when next logging on in Vista and Windows 7. Jan 14, 2011 at 9:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .