Batch / Bulk Replacing the server name on users home drives

I recently had to move users shares from one server to another and needed to update the location of every user home drive in Active Directory. It wasn’t possible to do this using the MMC snap-in as some shares had slightly different paths (ie, I couldn’t select all the users and put in \\newserver\users\%username%).

A powershell script worked quite well (source):

Import-Module ActiveDirectory

$oldServerName = "myoldserver2008"
$newServerName = "newserver2015"


[array]$AllUsers = Get-ADUser -Filter * -Properties HomeDirectory

foreach($user in $AllUsers){

	if(($user.HomeDirectory.ToString()).contains($oldServerName)){
		$homeDirectory = ($user.HomeDirectory.ToString()) -replace $oldServerName, $newServerName
		write-host $user.HomeDirectory.ToString() set to $homeDirectory
		Set-ADUser $user.DistinguishedName -HomeDirectory $homeDirectory
	}

}

If you wanted to test this code without changing the users just remove the “Set-ADUser” (below the write-host line) line from the above script and it will just write out the changes that it would perform to the screen without making the changes.

Ideally you should be using this method to move your user shares to a DFS location so “server name changes” are not required in the future and you can have redundant storage.

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

Comment on this topic

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