A vbs script, which runs once a month at login, to delete all the user directories (except for the Administrator, All Users and Default User directory) on the worksgtation of the users which doesn’t excist anymore in the active directory.

' VBScript Source File --
' NAME: CleanLocalProfiles.vbs
' AUTHOR: Mark D. MacLachlan , The Spider's Parlor
' URL : http://www.thespidersparlor.com
' 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.
' This script and many more can be found in the Admin Script Pack
' by The Spider's Parlor http://www.thespidersparlor.com/vbscript
On Error Resume Next
Path1 = "C:\Documents and Settings\"

Dim fso
Dim oFolder
Dim oFile
Dim oSubFolder

Set fso = createobject("Scripting.FileSystemObject")
Set objDomain = getObject("LDAP://rootDse")
DomainString = objDomain.Get("dnsHostName")
If Err <> 0 Then
'Domain not present to validate users against. Exit script to prevent data loss.
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)
UserString = oSubFolder.Name
End If

Select Case lcase(UserString)
Case "administrator", "all users", "default user", "localservice", "networkservice","markmac","markdmac"
'Do nothing
Case Else
'Now bind to the user object to see if a valid user. If not delete folder.

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


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



Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: