Solved

VBscript to delete a users account and move their home folder

Posted on 2011-02-17
3
592 Views
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

0
Comment
Question by:WNottsC
3 Comments
 
LVL 1

Accepted Solution

by:
raju1706 earned 500 total points
Comment Utility
Hi,

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
Const ADS_SCOPE_SUBTREE = 2

' 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)
    Err.Clear
    home_dir = objUser.homeDirectory
    
    objfso.MoveFolder home_dir,dest_dir_path
    
	set objContainer = getobject(objUser.Parent)
	objContainer.Delete "user","cn=" & objUser.cn
    
    If Err.Number = 0 Then
        WScript.Echo "User Account Deleted."
    Else
        WScript.Echo Err.number
        WScript.Echo Err.Desciption
    End If
    
Else
    
    WScript.Echo "user not found"
    
End If

Open in new window

0
 
LVL 12

Expert Comment

by:prashanthd
Comment Utility
Have you tried the above solution, it should work..
0
 

Author Closing Comment

by:WNottsC
Comment Utility
This has worked fine thanks
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Do you have users whose passwords are expiring and they are constantly calling you?  Well I sure did and needed a way to put an end to this.  We have a lot of remote users which would not be notified that their passwords were expiring since they wer…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …

772 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

9 Experts available now in Live!

Get 1:1 Help Now