0

Migrate Users Home Folder To A New File Server Using Powershell

Not too long ago I had a project to decommission an old 2008 R2 server, spin up a new Windows 2016 Server, migrate the data, migrate users and fix the mess of permissions that had been previously set by the admins before my time. This data was home to the users Home Folder so it was constantly being accessed and there was no time for any downtime.

The tasks that needed to be completed for each folder and each user.

  • Copy the data to new location with all the metadata in tact (Timestamps, attributes etc.. should remain the same).
  • Modify their Active Directory HomeDrive and HomeDirectory to the remap and point to the new location.
  • Set NTFS owner permissions on the new folder to .\Administrators.
  • Grant NTFS permissions to full control for that individual user only.
  • Test Access.

Since I was doing this for several hundred users, it would have taken me forever and a day to do this manually. I was looking for an automated and effective way to accomplish this task, and Powershell was right up my alley. Note, you will also need to download the NTFSSecurity Module. You can check out a post on that RIGHT HERE for usage and examples and such.

Migrate Users Home Folder To A New File Server Using Powershell

Function Migrate-UsersToNewHomeFolder {
#requires -Module ActiveDirectory
#requires -Module NTFSSecurity
#requires -RunAsAdministrator
#requires -Version 3.0

<#
.Synopsis
    Ths will update the Active Directory HomeDrive and HomeDirectory, Migrate the files using Robocopy and set the appriopriate permissions.
    For updated help and examples refer to -Online version.
 

.DESCRIPTION
    Ths will update the Active Directory HomeDrive and HomeDirectory, Migrate the files using Robocopy and set the appriopriate permissions.
    For updated help and examples refer to -Online version.


.NOTES   
    Name: Migrate-UsersToNewHomeFolder
    Author: The Sysadmin Channel
    Version: 1.0
    DateCreated: 2018-Jun-09
    DateUpdated: 2017-Jun-09

.LINK
    https://thesysadminchannel.com/migrate-users-home-folder-new-file-server-using-powershell/ -


.EXAMPLE
    For updated help and examples refer to -Online version.

#>

    [CmdletBinding()]
        param(
            [Parameter(
                Mandatory=$true,
                ValueFromPipeline=$true,
                ValueFromPipelineByPropertyName=$true,
                Position=0)]
            [string[]] $UserName,

            [string]   $OldServer = 'PAC-FS01',
            [string]   $NewServer = 'PAC-FS02',
            [string]   $Domain    = 'AD'
            
        )

    BEGIN {}

    PROCESS {
        foreach ($name in $UserName) {
            if ([bool](Get-ADUser -Filter {samaccountname -eq $name} )) {
                Get-ADUser $name | Set-ADUser -HomeDrive H: -HomeDirectory "\\$NewServer\Users\$name"
                robocopy "\\$OldServer\Users\$Name" "\\$NewServer\Users\$Name" /mir /copy:datou /r:1 /w:10
                sleep 2
                Set-NTFSOwner -Path "\\$NewServer\Users\$Name" -Account Administrators
                sleep 2
                Add-NTFSAccess -Path "\\$NewServer\Users\$Name" -Account "$Domain\$name" -AccessRights FullControl -AccessType Allow -AppliesTo ThisFolderSubfoldersAndFiles

            }
        }
    }

    END {}

}

So there you have it. Hopefully this article was able to help you migrate users from one home folder to another. If you liked this post be sure to check out TheSysadminChannel on Youtube. And if you’re looking to further your Powershell or SCCM knowledge, be sure to check out Learn Powershell In a Month Of Lunches Book for Powershell and Learn SCCM in a Month of Lunches Book for System Center Configuration Manager.

Migrate Users Home Folder To A New File Server Using Powershell
4.8 (96%) 10 votes

Paul Contreras

Hi, my name is Paul and I am a Sysadmin who enjoys working on various technologies from Microsoft, VMWare, Cisco and many others. Join me as I document my trials and tribulations of the daily grind of System Administration.