How to change home directory in Active Directory using VBS
Â
Change Home Directory
Â
A VBScript script in the Active Directory category.
Â
Use this script to go through all users and change their home directory. There is a filter string so it will only change those home directories you want changed:
Â
Â
Â
strNewPath: the full UNC of the new directory path (the script will automatically add the users username to the end
strSearchContext: script will scan the current home directory and if it finds a match to this string it will make the change.
Blank home directories are left untouched.
Â
Source Code
Important Note: This script has not been checked by Spice works. Please understand the risks before using it.
——————————————————————————————————————————-
Â
1.strNewPath      = "\sclsusers"
2.strSearchContext  = "\dx-file1"
3.
4.
5.
6.Dim rootDSE, domainObject
7.Set rootDSE = GetObject("LDAP://RootDSE")
8.domainContainer = rootDSE.Get("defaultNamingContext")
9.Set domainObject = GetObject("LDAP://" & domainContainer)
10.
11.Set fs = CreateObject ("Scripting.FileSystemObject")
12.Set logFile = fs.CreateTextFile (".ChgHomeDir.log")
13.
14.const crlf="<BR>"
15.Set objExplorer = WScript.CreateObject("InternetExplorer.Application")
16.objExplorer.Navigate "about:blank" Â Â
17.objExplorer.ToolBar = 0
18.objExplorer.StatusBar = 0
19.objExplorer.Width = 500
20.objExplorer.Height = 300Â
21.objExplorer.Left = 100
22.objExplorer.Top = 100
23.
24.Do While (objExplorer.Busy)
25. Â Wscript.Sleep 200
26.Loop
27.
28.objExplorer.Visible = 1 Â Â
29.txtOutput=""
30.
31.
32.If Right(strNewPath, 1) <> "" then
33. Â strNewPath = strNewPath & ""
34.End If
35.strSearchContext = UCase(strSearchContext)
36.
37.exportUsers(domainObject)
38.
39.
40.Set oDomain = Nothing
41.showText("FINISHED!!")
42.WScript.Quit
43.
44.
45.
46.Sub ExportUsers(oObject)
47. Â Dim oUser
48. Â For Each oUser in oObject
49. Â Â Select Case oUser.Class
50. Â Â Â Â Case "user"
51. Â Â Â Â Â Â If oUser.homeDirectory <> "" and InStr(UCase(oUser.homeDirectory), strSearchContext) > 0 then
52. Â Â Â Â Â Â Â showText(oUser.displayName)
53. Â Â Â Â Â Â Â logFile.WriteLine oUser.homeDirectory & "," & strNewPath & oUser.sAMaccountName
54. Â Â Â Â Â Â Â oUser.Put "homeDirectory", strNewPath & oUser.sAMaccountName Â
55. Â Â Â Â Â Â Â oUser.SetInfo
56. Â Â Â Â Â Â End if
57. Â Â Â Â Case "organizationalUnit" , "container"
58. Â Â Â Â Â Â ExportUsers(oUser)
59. Â Â Â End select
60. Â Next
61. End Sub
62.
63.
64.Sub ShowText(txtInput)
65. Â txtOutput = "Change Home Directory Path" & crlf
66. Â txtOutput = txtOutput & "==========================" & crlf & crlf
67. Â txtOutput = txtOutput & "Search Context: " & strSearchContext & crlf
68. Â txtOutput = txtOutput & "New Home Dir Path: " & strNewPath & crlf & crlf
69. Â txtOutput = txtOutput & "Working on: Â " & txtInput
70. Â objExplorer.Document.Body.InnerHTML = txtOutput
71.End Sub
Â
Thanks