Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I am using Windows XP and would like to create a batch file that deletes all user data folders except for All Users and Default User. How can I achieve this?

share|improve this question
    
Do you mean that you want to delete all the User profiles on a given machine, including their folder in the Docs and Settings folder? –  Izzy Aug 19 '09 at 20:47
    
I just need them out of the docs and settings folder. –  Nathan Lawrence Aug 19 '09 at 20:48
    
The cleanest way is to delete the profile - want a script I made? –  Izzy Aug 19 '09 at 20:49
    
Is there a way to exclude certain users? –  Nathan Lawrence Aug 19 '09 at 20:51
    
Yes - see answer –  Izzy Aug 19 '09 at 20:53

2 Answers 2

First of all you don't want to delete the folders at OS level, instead you should be using delprof to accomplish this. Secondly, you can't delete the profile of a currently logged on user, so plan to run it as a computer startup script or run it remotely against a machine you know is switched on but doesn't have a user currently logged on.

So you need to have a look at this KB article which will provide you with all the info you need. The specific command you want will be something like:

delprof /q /i /c:computername
share|improve this answer
1  
I think you can delete all profiles like that with additionally specifying 0 or 1 as the inactive days for profile. /d:days: This parameter specifies the number of days after which Delprof.exe considers the profiles as inactive. Profiles that have not been used for a period of time that is longer than the specified value are deleted. –  KAPes Oct 1 '09 at 3:21

I wrote this for another user with a different problem, but it'll do what you want (and is much cleaner than your suggestion of just deleting the Users folder).

It's a VBScript, so just save as a .vbs file and double click. It'll ask for the Fully Qualified Domain Name of the PC you want to inspect. It'll then list each User Profile on that machine, and give you the choice of deleting the profile (and thus the Users folder).

If you are getting permissions problems, change the UserName = "" and Password = "" lines to an account that has Local Admin rights over the target PC.

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
share|improve this answer
    
I can't get it to list. It just says profile found. I tell it to delete, then it closes. –  Nathan Lawrence Aug 19 '09 at 21:04
    
It just says Profile Found, and doesn't list any details, like the Local Path? –  Izzy Aug 19 '09 at 21:09
    
PS. It won't produce one big list of profiles. It'll cycle through them. Found Profile 1, Delete? Found Profile 2, Delete? etc –  Izzy Aug 19 '09 at 21:13

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.