VBscript to delete a users account and move their home folder

Posted on 2011-02-17
Last Modified: 2012-05-11
I have a vbscript that works perfectly well at disabling an account and removing all group membership apart from the Primary Group.

What I would now like to do is delete these accounts and move the attached home folder to a new location

Question by:WNottsC

Accepted Solution

raju1706 earned 500 total points
ID: 34915894

Please try below code
'On Error Resume Next

struser = staffid
dest_dir_path=""   '"\\servername\drive$\deleteduser" Mention full path

Dim objRootDSE, strDomain, strUsername, objConnection, objCommand, objRecordSet, strDN

' Get domain components
Set objRootDSE = GetObject("LDAP://RootDSE")
strDomain = objRootDSE.Get("DefaultNamingContext")
Set objFSO = CreateObject("Scripting.FileSystemObject")

' Set ADO connection
Set objConnection = CreateObject("ADODB.Connection")
objConnection.Provider = "ADsDSOObject"
objConnection.Open "Active Directory Provider"

' Set ADO command
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.Properties("Searchscope") = ADS_SCOPE_SUBTREE

objCommand.CommandText = "SELECT distinguishedName FROM 'LDAP://" & strDomain & "' WHERE objectCategory='person' AND samaccountname = '" & struser & "'"

' Set recordset to hold the query result
Set objRecordSet = objCommand.Execute

If Not objRecordSet.EOF Then
    strDN = "LDAP://" & objRecordSet.Fields("distinguishedName").Value
    Set objUser = GetObject(strDN)
    home_dir = objUser.homeDirectory
    objfso.MoveFolder home_dir,dest_dir_path
	set objContainer = getobject(objUser.Parent)
	objContainer.Delete "user","cn=" &
    If Err.Number = 0 Then
        WScript.Echo "User Account Deleted."
        WScript.Echo Err.number
        WScript.Echo Err.Desciption
    End If
    WScript.Echo "user not found"
End If

Open in new window

LVL 12

Expert Comment

ID: 34941102
Have you tried the above solution, it should work..

Author Closing Comment

ID: 34941517
This has worked fine thanks

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  ( Here (http…
Restoring deleted objects in Active Directory has been a standard feature in Active Directory for many years, yet some admins may not know what is available.
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now