Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Delete Local User Directories

Status
Not open for further replies.

markdmac

MIS
Dec 20, 2003
12,340
US
Follow up to thread329-1237896

Modified code that should work more universally

Code:
'==========================================================================
'
' VBScript Source File --
'
' NAME: CleanLocalProfiles.vbs
'
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL   : [URL unfurl="true"]http://www.thespidersparlor.com[/URL]    
' Copywrite (c) 2006 All rights reserved
' DATE  : 06/2/2006
'
' COMMENT: Cleans the local user profiles directory.
'          Checks for current users and deletes folders
'          of users no longer in AD.  
'          Use with caution, ensure you have a good backup.  Data loss may occur.
' MODIFICATIONS:
'          Modified to find base user directory folder.  Corrections for binding to user object.
'          This script and many more can be found in the Admin Script Pack
'          by The Spider's Parlor [URL unfurl="true"]http://www.thespidersparlor.com/vbscript[/URL]
'=====================================
On Error Resume Next

Dim WshShell, fso, oFolder, oFile, oSubFolder

Set WshShell = WScript.CreateObject("WScript.Shell")
strDesktop = WshShell.SpecialFolders("AllUsersDesktop")
pathArray = Split(strDesktop,"\")
Path1 = pathArray(0) & "\" & pathArray(1) & "\"


Set fso = CreateObject("Scripting.FileSystemObject")
Set objDomain = GetObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
If Err.Number <> 0 Then
   'Domain not present to validate users against.  Exit script to prevent data loss.
   WScript.Quit
End If

Set oFolder = fso.GetFolder(Path1)
Set colSubfolders = oFolder.Subfolders

For Each oSubfolder in colSubfolders
    'First check To see if the folder is from multiple profiles in form username.domainname
    'Use the folder name to get the user ID
    If InStr(oSubFolder.Name,".") Then
       splitArray = Split(oSubFolder.Name,".")
       UserString = splitArray(0)
    Else
       UserString = oSubFolder.Name
    End If
    
    Select Case lcase(UserString)
        Case "administrator", "all users", "default user", "localservice", "networkservice"
             'Do nothing
        Case Else
               'Now bind to the user object to see if a valid user.  If not delete folder.

               Set UserCheck = GetObject("WinNT://" & DomainString & "/" & UserString)
            If Err.Number <> 0 Then
                fso.DeleteFolder(Path1 & oSubFolder.Name),True
                WScript.Echo oSubFolder.Name
            End If
    End Select
    
Next

Set oSubFolder = Nothing
Set oFolder = Nothing
Set fso = Nothing

I hope you find this post helpful.

Regards,

Mark

Check out my scripting solutions at
Work SMARTER not HARDER. The Spider's Parlor's Admin Script Pack is a collection of Administrative scripts designed to make IT Administration easier! Save time, get more work done, get the Admin Script Pack.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top