Bulk Migrate User Home Directory in Active Directory


This summer I’m decommissioning an old file server that is no longer under warranty, and we have plans for it along with the iSCSI SAN it’s connected to. Slight problem though – even though I’ve configured our user shares to replicate around using DFS, for years the user creation script that was used for creating accounts hard coded the user home directory to this file server. With thousands of accounts – I’m not about to go and do all this by hand.

Enter Quest ActiveRoles Management Shell (free). Sure I could have used other tools – but I’ve got this one and I like it. Anyway, here’s the script I wrote to do this. Note:  It is likely not perfect, maybe not the most efficient way to handle this (definitely a bit slow), but it works and was ran against a few thousand user accounts.

#
# UserHomeDirMigration.ps1
# User migration script to move a user's home directory
# from old file server to DFS Users share
#

# Add Snap-Ins
Add-PSSnapin Quest.ActiveRoles.ADManagement

# Set the working OU
set-variable -name OU -value "OU_Users_Are_In"

# Pause on error
function Failure {
			Write-Host "Press any key to continue ..."
			$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
			}

Get-QADUser -SearchRoot $OU | Foreach-Object{ 
	$user = Get-QADUser -Identity $_
	$user | select LogonName
	#$user | select HomeDirectory 
	$nullTest = $user.HomeDirectory
	
	# Check for an empty drive
	if ($nullTest) {
		# Validate if user drive is old file server
		if ($user.HomeDirectory.ToLower().Contains("OLDSERVER")) {
			#Write-Host -ForegroundColor GREEN "TRUE - OLDSERVER"
			#Write-Host -ForegroundColor YELLOW $user.HomeDirectory
			$finalHome = $user.HomeDirectory.ToLower().Replace("OLDSERVER", "NEWSERVER")
			#Write-Host -ForegroundColor YELLOW $finalHome
			Set-QADUser -Identity $_ -HomeDirectory $finalHome
			# User drive is something other than old server
			} elseif ($user.HomeDirectory.Contains("NEWSERVER")) {
			Write-Host -ForegroundColor GREEN "Already Changed to NEWSERVER"
			Failure
			} else {
			Write-Host -ForegroundColor RED   "Unknown Value ---- ", $user.HomeDirectory
			Failure
			}
		} else {
		# User drive is empty
		Write-Host -ForegroundColor RED   "Unknown Value - Likely Null"	
		Failure
	}
	# A couple extra spaces in between users
	Write-Host
	Write-Host
}
Advertisements

Leave a Reply

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