2

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.

4.7/5 - (12 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.

2 Comments

  1. This is an enhanced script you can use with a TXT file. Just put the names line by line into the TXT file and run the script with your custom server names and domain name. Change the drive letter to one you like.

    Import-Module ActiveDirectory
    Import-Module NTFSSecurity

    Function Migrate-UsersToNewHomeFolder {
    [CmdletBinding()]
    param(
    [string[]] $UserName,
    [string] $OldServer = ‘OldServerName’,
    [string] $NewServer = ‘NewServerName’,
    [string] $Domain = ‘domain.com’,
    [string] $UserListFile
    )

    BEGIN {
    Import-Module ActiveDirectory
    Import-Module NTFSSecurity
    }

    PROCESS {
    if ($UserListFile -eq $null) {
    Write-Error “Please provide a User file.”
    return
    }

    $UserList = Get-Content $UserListFile

    foreach ($name in $UserList) {
    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
    Start-Sleep -Seconds 2
    Set-NTFSOwner -Path “\\$NewServer\Users\$name” -Account Administrators
    Start-Sleep -Seconds 2
    Add-NTFSAccess -Path “\\$NewServer\Users\$name” -Account “$Domain\$name” -AccessRights FullControl -AccessType Allow -AppliesTo ThisFolderSubfoldersAndFiles
    }
    }
    }

    END {}
    }

  2. What is required in the Username String. I have entered a name in [] but when i run the script nothing happens. I would like to use a csv file with the user names .

    Also i have change the security options

    This is what i have

    [CmdletBinding()]
    param(
    [Parameter(
    Mandatory=$true,
    ValueFromPipeline=$true,
    ValueFromPipelineByPropertyName=$true,
    Position=0)]
    [string[]] $UserName = [username], ## Can we user a .csv file here ??

    [string] $OldServer = ‘oldserver’,
    [string] $NewServer = ‘newserver’,
    [string] $Domain = ‘Domain’

    )

    BEGIN {}

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

    }
    }
    }

    END {}

    }

Leave a Reply

Your email address will not be published. Required fields are marked *