Announcement

Collapse
No announcement yet.

Delete user's profiles

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Delete user's profiles

    I work at a university, where we have 600+ users, 60 Windows XP Pro and a Windows Server 2003.
    The thing is that any user logs onto any computer at any time, so after a few days I have several (up to 100) user profiles under c:\documents and settings\(username).
    Sometimes they store huge amounts of data, and I end up with filled HDs, even though all the computers have 80 GB drives.
    All the user names are 5 and 6 digit numbers. For example, I get c:\documents and settings\123456\.

    Is there any way to automate the deletion of those profiles instead of doing it manually?
    I thought of running a .bat file that has something like:

    deltree c:\documents and settings\1*
    deltree c:\documents and settings\2*
    deltree c:\documents and settings\3*
    ...and so on up to number 9.

    But I have to run that on every computer "manually", and files would be deleted instead of going to the recycle bin, which would be a plus.

    Any ideas?

    I haven't tried the .bat idea... but I guess it should work.

  • #2
    Re: Delete user's profiles

    Try running this vbscript

    Code:
    '=========================================================================================
    ' CleanProfiles.vbs
    ' Version: 1.03
    ' Last Update: 2006.04.08
    '=========================================================================================
    ' Purpose:
    ' Delete profile left-over folders from the local servers
    '=========================================================================================
    ' Syntax:
    ' ccscript CleanProfiles.vbs
    '=========================================================================================
    
    Option Explicit
    Const EVENT_SUCCESS = 0, EVENT_ERROR = 1
    Dim arrSafeProfiles(6), iLogType: iLogType = 0
    Dim sOutPut, iSkipped, iDeleted, iErrors
    iSkipped = 0: iDeleted = 0: iErrors = 0
    
    arrSafeProfiles(0) = "All Users"
    arrSafeProfiles(1) = "Default User"
    arrSafeProfiles(2) = "LocalService"
    arrSafeProfiles(3) = "NetworkService"
    arrSafeProfiles(4) = "Administrator"
    arrSafeProfiles(5) = WScript.CreateObject("WScript.Network").ComputerName
    arrSafeProfiles(6) = WScript.CreateObject("WScript.Network").UserName
    
    DeleteProfiles(ProfilesFolder)
    SummaryAction
    EventLog iLogType, sOutPut
    
    Sub DeleteProfiles(sProfilesFolder)
    	On Error Resume Next
    	Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    	Dim oProfilesFolder: Set oProfilesFolder = oFSO.GetFolder(sProfilesFolder)
    	Dim oFolders: Set oFolders = oProfilesFolder.SubFolders
    	Dim oFolder, sFolder2Delete
    	For Each oFolder In oFolders
    		If Not IsSafeProfile(oFolder.Name) Then
    			sFolder2Delete = oFolder.Name
    			LogEntry " + Deleting Profile '" & sFolder2Delete & "'"
    			Err.Clear
    			oFSO.DeleteFolder oFolder.Path, True
    			If Err.Number = 0 Then
    				iDeleted = iDeleted + 1
    				LogEntry "    '" & sFolder2Delete & "' deleted successfully." & vbNewLine
    			Else
    				iLogType = 1
    				iErrors = iErrors + 1
    				LogEntry "    Can't delete '" & sFolder2Delete & "' (Error " & Err.Number & " - " & Err.Description & ")" & vbNewLine
    			End If
    		Else
    			iSkipped = iSkipped + 1
    			LogEntry " - Skipping Profile '" & oFolder.Name & "'" & vbNewLine
    		End If
    	Next
    	Set oFolders = Nothing
    	Set oProfilesFolder = Nothing
    	Set oFSO = Nothing
    End Sub
    
    Function IsSafeProfile(sProfile)
    	On Error Resume Next
    	Dim sData, arrLines, sLine, sUser, i, bFlag: bFlag = False
    	sData = Cmd("query session | find " & Chr(34) & "rdpwd" & Chr(34) & " |find " & Chr(34) & "rdp-tcp#" & Chr(34))
    	arrLines = Split(sData, vbNewLine)
    	If UBound(arrLines) > 0 Then
    	For Each sLine In arrLines
    		sUser = Trim(Mid(sLine,20,20))
    		If sUser <> "" Then
    			If InStr(UCase(sProfile), UCase(sUser)) > 0 Then
    				LogEntry "   User '" & sUser & "' is logged in!"
    				bFlag = True
    				Exit For
    			End If
    		End If
    	Next
    	Else
    		LogEntry "   Can't check if '" & sProfile & "' is logged in!"
    		iLogType = 1
    		bFlag = True
    	End If
    	For i = 0 To UBound(arrSafeProfiles)
    		If InStr(UCase(sProfile), UCase(arrSafeProfiles(i))) > 0 Then
    			LogEntry "   Profile '" & sProfile & "' is in the safe profiles list!"
    			bFlag = True
    			Exit For
    		End If
    	Next
    	IsSafeProfile = bFlag
    End Function
    
    Function Cmd(sCommandLine)
    	On Error Resume Next
    	Dim sFileOut, oFileOut, sCmd
    	Dim oWshShell: Set oWshShell = CreateObject("WScript.Shell")
    	Dim oFSO: Set oFSO = CreateObject("Scripting.FileSystemObject")
    	sFileOut = oFSO.GetTempName
    	sCmd = "%COMSPEC% /c " & sCommandLine & " >" & sFileOut
    	oWshShell.Run sCmd, 0, True
    	If oFSO.FileExists(sFileOut) Then
    		If oFSO.GetFile(sFileOut).Size > 0 Then
    			Set oFileOut = oFSO.OpenTextFile(sFileOut)
    			Cmd = oFileOut.Readall
    			oFileOut.Close
    		End If
    		oFSO.DeleteFile(sFileOut)
    	End If
    	Set oFileOut = Nothing
    	Set oFSO = Nothing
    	Set oWshShell = Nothing
    End Function
    
    Function ProfilesFolder()
    	Dim sProfilesFolder
    	Dim oWshShell: Set oWshShell = WScript.CreateObject("WScript.Shell")
    	Dim oWshSysEnv: Set oWshSysEnv = oWshShell.Environment("Process")
    	sProfilesFolder = oWshSysEnv("USERPROFILE")
    	sProfilesFolder = Left(sProfilesFolder,InStrRev(sProfilesFolder,"\"))
    	ProfilesFolder = sProfilesFolder
    	Set oWshSysEnv = Nothing
    	Set oWshShell = Nothing
    End Function
    
    Sub SummaryAction
    sOutPut = vbNewLine & sOutPut & _
    			"   Actions Summary:" & vbNewLine & _
    			"   -------------------------------------------" & vbNewLine & _
    			"   " & iSkipped & " profile(s) skipped." & vbNewLine & _
    			"   " & iDeleted & " profile(s) deleted." & vbNewLine & _
    			"   " & iErrors & " error(s) encountered." & vbNewLine
    End Sub
    
    Sub LogEntry(sEntry)
    	sOutPut = sOutPut & sEntry & vbNewLine 
    End Sub
    
    Sub EventLog(EventType, sEntry)
    	Dim oShell: Set oShell = Wscript.CreateObject("Wscript.Shell")
    	oShell.LogEvent EventType, WScript.ScriptName & " status:" & _
    						vbNewLine & vbNewLine & sEntry
    	Set oShell = Nothing
    End Sub
    Or If you want something more simple, try this batch:
    Code:
    @echo off
    Set BaseFolder="C:\Documents and Settings\"
    Set counter=0
    Set MaxNum=10000
    
    :myLoop
    set /a counter=%counter%+1
    
    title Cleanning Documents and Settings (Folder %counter%)...
    
    rem del %BaseFolder%%counter%\*.* /s /f /q
    rd /s /q %BaseFolder%%counter%
    
    if %counter%==MaxNum goto ExitLoop
    
    goto myLoop
    
    :ExitLoop
    pause
    Just change the MaxNum variable to match your needs.

    You can create a Scheduled Task and have it run every night.
    Last edited by martin77; 23rd September 2007, 08:16.

    Comment


    • #3
      Re: Delete user's profiles

      Maybe needless to say,
      but these script must run as a local administrator. And it cannot delete its own profile folder.
      That is why it is better to run the script as a computer startupscript (not a userlogon script).


      Aan other example is by using a regular expression
      batch version:
      Code:
      Set Target=%AllUsersProfile:~0,-10%
      For /F %%a In ('Dir /AD /B "%Target%" ^|Findstr /B /I /R "[0-9][0-9][0-9][0-9][0-9]"  ') Do RD "%Target%\%%a" /S /Q
      It batch only woks in Win XP and newer. It searches in ?:\Documents and Settings to all folders the name starting with 5 numbers. And delete these folders

      \Rems

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: Delete user's profiles

        Hi,
        Would this code work as a script to delete all documents and settings apart from 'all users','default user','localservice','networkservice' and 'administrator' ?
        If it would work would it only work when an administrator logs on or would it also work if a user/student logged in too ?

        Code:
        Dim arrFolders()
        intSize = 0
        
        arrSafeProfiles(0) = "All Users"
        arrSafeProfiles(1) = "Default User"
        arrSafeProfiles(2) = "LocalService"
        arrSafeProfiles(3) = "NetworkService"
        arrSafeProfiles(4) = "Administrator"
        
        strComputer = "."
        Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
        
        strFolderName = "c:\scripts\archive"
        
        Set colSubfolders = objWMIService.ExecQuery _
            ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
                & "Where AssocClass = Win32_Subdirectory " _
                    & "ResultRole = PartComponent")
        
        ReDim Preserve arrFolders(intSize)
        arrFolders(intSize) = strFolderName
        intSize = intSize + 1
        
        For Each objFolder in colSubfolders
            GetSubFolders strFolderName
        Next
        
        Sub GetSubFolders(strFolderName)
            Set colSubfolders2 = objWMIService.ExecQuery _
                ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
                    & "Where AssocClass = Win32_Subdirectory " _
                        & "ResultRole = PartComponent")
        
            For Each objFolder2 in colSubfolders2
                strFolderName = objFolder2.Name
                ReDim Preserve arrFolders(intSize)
                arrFolders(intSize) = strFolderName
                intSize = intSize + 1
                GetSubFolders strFolderName
            Next
        End Sub
        
        For i = Ubound(arrFolders) to 0 Step -1
            strFolder = arrFolders(i)
            strFolder = Replace(strFolder, "\", "\\")
            Set colFolders = objWMIService.ExecQuery _
                ("Select * from Win32_Directory where Name = '" & strFolder & "'")
        
            For Each objFolder in colFolders
                errResults = objFolder.Delete
            Next
        Next
        Thanks.
        Daniel Askin.

        Comment

        Working...
        X