Migrating user home directories can be a tedious and time-consuming task for IT administrators. In this blog post, we will provide a PowerShell script that automates the process of copying user home directories from one location to another, while also updating the home directory path in Active Directory.
The script uses the robocopy command, which is a built-in command in Windows, to copy the home directories. Robocopy is a powerful tool that can copy files and folders, including permissions. This is important when migrating home directories as it ensures that the permissions for the users remain intact in the new location.
The script starts by importing the Active Directory module, which allows it to interact with Active Directory. Next, it defines the source and destination directories. In this example, the destination directory is "\\MyServer\NewHomeDirectories".
It then retrieves a list of all active users from Active Directory using the Get-ADUser command. The script loops through each user, retrieves their home directory path, and constructs the new home directory path using the user's SamAccountName.
Finally, the script uses robocopy to copy the home directory to the new location, including the permissions using the /E and /COPYALL options. It then updates the home directory path in Active Directory using the Set-ADUser command.
It is important to note that you need to have the appropriate permissions to read and copy the home directories and make changes in Active Directory. Additionally, it is recommended to test this script in a test environment before running it in a production environment.
Here is the script for your reference:
# Import the Active Directory module Import-Module ActiveDirectory # Define the new destination directory $dstDir = "\\MyServer\NewHomeDirectories" # Get all active users from Active Directory $users = Get-ADUser -Filter {Enabled -eq $True} # Loop through each user foreach ($user in $users) { # Get the home directory path for the user $homeDir = $user.HomeDirectory # Construct the new home directory path $newHomeDir = $dstDir + "\" + $user.SamAccountName # Copy the home directory to the new location, including permissions robocopy $homeDir $newHomeDir /E /COPYALL # Update the home directory path in Active Directory Set-ADUser -Identity $user.DistinguishedName -HomeDirectory $newHomeDir }
This PowerShell script can save IT administrators a lot of time and effort when migrating user home directories. By automating the process of copying home directories, including permissions, and updating the home directory path in Active Directory, this script can help ensure a smooth and successful migration.