We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Modify Home Drive & TS Home Drive AD Attributes

MAJAEJ asked
Medium Priority
Last Modified: 2012-06-27
I am looking for a script to modify a number {not all} of users Home Drive  and TS Home Drive location.  {ie: we're moving their home drives}.

User names can be read from an Excell SS or .CSV file .TXT file - don't really care.

But I need to:
1) Read username
2) Set username attributes to update HomeDirectory and TSHomeDirectory..

Your timely response would be GREATLY appreciated.


Watch Question

Hi there

I have written a script for you to do what you want.
You need to have the user logon names 1 per line in a text file somewhere

There are settings you need to change in the script but all of them are between the borders. They should be self explanatory as to what you need to change them to.

Save this script notepad as a .vbs file
Run from the command prompt as
CScript scriptName.vbs

This si becuase it will output the results as we go along to the command window instead of dozens of popup prompts

Hope it is what you are looking for


Most people try to find the Terminal Services Attributes in ADSI and will never find them. This is due to the fact that all the settings for the Terminal Service Profile, Session, Remote Control and Environment tabs are all stored in a single attribute called User-Parameters. This attribute is a binary attribute and cannot be read using the normal ADSI tools.

Since Windows 2003 Server there is a DLL file (TsUserEx.dll) that makes reading this attribute easy by binding to the User object first. Then we can read and write to this attribute via a wrapper which is the TsUserEx.dll.
By installing the "Windows 2003 Server Administration Tools Pack" you will get this DLL installed.

Option Explicit
	Call SetTerminalServicesSettings
Sub SetTerminalServicesSettings
' Version 1.0
' Written By Krystian Karia
' Dated 15-Feb-2009
' Reads  a list  of  user  logon  names from
' a text file  and then  searches  for  them
' in AD. We then check if  they  have a home
' Driectory  set  and  and TS Home Directory
' set and  report their current settings. If
' the Change Flag is set we also change them
' according  to  the preferences set between
' then borders below.
' Catch errors ourselves
	On Error Resume Next 
' Declare Variables and Constants
	Dim fso, oFile
	Dim oRoot, objConnection, objCommand, objRecordSet, objUser
	Dim strOldHome, strOldTSHome, strReadResults, arrUser
	Dim blnChangeFlag, blnFileHasHeader, blnTSEnabled
	Dim strHomeDirectory, strTSHomeDirectory
	Dim strBase, strQuery, sFileLocation
	Dim strHomeDrive, strTSHomeDrive
	Const ForReading = 1
'**************************************** PREFERENCES ****************************************
' <<<<< ONLY CHANGE SETTINGS BELOW (Keep the Quotes) >>>>>
	sFileLocation = "C:\UserList.txt"					'<________Contains 1 user per line (must be their Logon Name)
	blnChangeFlag = False								'<________True = Make the changes and False = Simulate changes
	blnFileHasHeader = False							'<________True = UserList has a Header and False = UserList has no Header
' User Home Drive Settings
	strHomeDrive = "H:"									'<________Set the Drive Letter to suit
	strHomeDirectory = "\\FileSvr1\HomeDrives\"			'<________Server you currently have it - must end in \
' User Terminal Services Home Drive Settings
	blnTSEnabled = 1									'<________1 = Enabled and 0 = Disabled 
 	strTSHomeDrive = "H:"								'<________Set the Drive Letter to suit
 	strTSHomeDirectory = "\\FileSvr1\TSHomeDrives\"		'<________Server you currently have it - must end in \
' <<<<< ONLY CHANGE SETTINGS ABOVE (Keep the Quotes) >>>>>
'**************************************** PREFERENCES ****************************************
' Create Objects
	Set fso = CreateObject("Scripting.FileSystemObject")
' Check Import UserList file exists
	If fso.FileExists(sFileLocation) Then
		Set oFile = fso.OpenTextFile(sFileLocation, ForReading, False)
			If Err.Number <> 0 Then
				ShowProgress "Error opening UserList: " & sFileLocation
			End If
		ShowProgress "UserList doesn't exist: " & sFileLocation
	End If
' Read the whole contents of the File
	Do While oFile.AtEndOfStream <> True
		strReadResults = oFile.ReadAll
	oFile.Close				' Close the file
	Set oFile = Nothing		' Release the object from memory
' Create an array from contents of the file if not blank file
	If Trim(strReadResults) <> "" Then
		arrUser = Split(strReadResults, vbNewLine)
	End If
' Is Header in file
	If blnFileHasHeader = True Then
	End If 
' Loop each user from the list in the file
	For i = INT_LINE_START To UBound(arrUser)
		If arrUser(i) = "" Then
			Exit For
		End If 
		ShowProgress arrUser(i)
' Dynamically get the Domain you are currently in
		Set oRoot = GetObject("LDAP://rootDSE")
			strBase = oRoot.Get("defaultNamingContext")
' Open a connection to Active Directory Database
		Set objConnection = CreateObject("ADODB.Connection")
			objConnection.Open "Provider=ADsDSOObject;"
		Set objCommand = CreateObject("ADODB.Command")
			objCommand.ActiveConnection = objConnection
				If Err.Number <> 0 Then
					ShowProgress "Error opening a connection to AD"
					ShowProgress Err.Number & ": " & Err.Description
				End If
			' Clear variable ready for re-use
				strQuery = ""
				strQuery = strQuery & "<LDAP://" & strBase & ">;"
				strQuery = strQuery & "(&(objectCategory=person)(objectClass=user)"
						strQuery = strQuery & "(sAMAccountName=" & arrUser(i) & "));"
				strQuery = strQuery & "sAMAccountName,DisplayName,ADsPath,homeDirectory;"
				strQuery = strQuery & "subtree"  
			objCommand.CommandText = strQuery
		Set objRecordSet = objCommand.Execute
				If Err.Number <> 0 Then
					ShowProgress "Error opening a Recordset to AD"
					ShowProgress Err.Number & ": " & Err.Description
				End If
' Check if any records were returned			
		If objRecordSet.RecordCount = 0  Then ' User not found in AD
			ShowProgress "No records found for user: " & arrUser(i)
			Do Until objRecordset.EOF
				Set objUser = GetObject(objRecordset.Fields("ADsPath"))
					If Err.Number <> 0 Then
						ShowProgress "Error binding to user: " & arrUser(i)
						ShowProgress Err.Number & ": " & Err.Description
					End If
' Check if a Home Directory exists
				If Not IsNull(objRecordset.Fields("homeDirectory")) Then
					strOldHome = objRecordset.Fields("homeDirectory")
						ShowProgress "Old Home: " & strOldHome
					ShowProgress "No Home Drive Exists for User: " & arrUser(i)
				End If 'IsNull
' Check if a TSHome Directory exists
 				If Not IsNull(objUser.TerminalServicesHomeDirectory) Then
					strOldTSHome = objUser.TerminalServicesHomeDirectory
						ShowProgress "Old TS Home: " & strOldTSHome
 					ShowProgress "No TS Home Drive Exists for User: " & arrUser(i)
 				End If 'IsNull
' Make the changes if flag set to True
				If blnChangeFlag = True Then 
				' Change the Home Drive Settings
					If strOldHome <> "" Then
						objUser.Put "homeDirectory", strHomeDirectory & arrUser(i)
						objUser.Put "homeDrive", strHomeDrive
						objUser.Put "homeDirectory", strHomeDirectory & arrUser(i)
					End If
						If Err.Number <> 0 Then
							ShowProgress "Error changing the Home Drive details"
							ShowProgress Err.Number & ": " & Err.Description
						End If
				' Change the TS Home Drive Settings
					If strOldTSHome <> "" Then
						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
						objUser.TerminalServicesHomeDrive = strHomeDrive
						objUser.TerminalServicesHomeDirectory = strHomeDirectory & arrUser(i)
						objUser.AllowLogon = blnTSEnabled
					End If
						If Err.Number <> 0 Then
							ShowProgress "Error changing the TS Home Drive details"
							ShowProgress Err.Number & ": " & Err.Description
						End If
					ShowProgress "Change not made - change the flag to True if you want changes to take effect"
				End If 
				Set objUser = Nothing
		End If
	Next ' i
End Sub
Sub ShowProgress(sComments)
	WScript.Echo sComments
End Sub

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


Wow - way more than I was expecting. Thank you.
I'll take this apart many times over to learn from it I am sure.

Thank you...

Hi, your welcome.
Thanks for the grade and points.

I wonder if the fact this post was left for so long was down to the Terminal Services attributes (or lack of them should i say). Even i was baffled for quite a while till i did some research. :-)

Anyway, thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.