Script to robocopy azure file share contents to new azure file share

If you need to move to a different azure file share, for example when you migrate from azure file shares using standard LRS storage account to a premium storage account, then there is no easy migration path. It’s your responsibility to move, copy or sync all of the files and directories and also to make sure to correctly transfer NTFS ACLs and ownership information.

There are different ways to accomplish the task. One straight forward way is using good old robocopy.

Robocopy is capable of transferring all files and folders and also ensures NTFS ACLs and ownership information is copied.

Also make sure azure file shares are mounted using storage account key otherwise robocopy will fail to copy ownership information for files and folder.

The following powershell script can be used to mount two azure file shares and copy all contents from source to target.

$ErrorActionPreference = "Stop"

function MountAzFileShare {
    param(
        [string]$unc,
        [string]$username,
        [string]$password,
        [string]$driveLetter
    )

    $pass_as_secure_string = ConvertTo-SecureString -AsPlainText $password -Force
    $credential = New-Object -Type System.Management.Automation.PSCredential -ArgumentList $username,$pass_as_secure_string
    New-PSDrive -Name $driveLetter -PSProvider FileSystem -Root $unc -Credential $credential
}

# source
$s_unc = "\\storstan01.file.core.windows.net\myshare"
$s_username = "localhost\storstand01"
$s_password = "IjdieMjdbe8djeIkdjd8jkdjke9jejecmUue31k5ed+PoidMjdhgfee738d+WjdhedkdlK8dj6eN323H6dTweM=="
$s_driveLetter = "S"
$s_robo = $s_driveLetter + ":"

# target
$t_unc = "\\storprem01.file.core.windows.net\myshare"
$t_username = "localhost\storprem01"
$t_password = "TehdNkaOslk9VdaheuZe6890jdke1JdTzjdN72df0O+LksmdndzetZsj88m+7Zdze8ndHdjhdoeu9edhOHgsnd=="
$t_driveLetter = "T"
$t_robo = $t_driveLetter + ":"

# mount source file share
MountAzFileShare -unc $s_unc `
-username $s_username `
-password $s_password `
-driveLetter $s_driveLetter

# mount target file share
MountAzFileShare -unc $t_unc `
-username $t_username `
-password $t_password `
-driveLetter $t_driveLetter

# copy all files and folders including NTFS ACLs and owner information
robocopy $s_robo $t_robo /E /B /COPY:DATSO /DCOPY:DAT /IT /R:1 /W:1

# unmount file shares
Get-PSDrive $s_driveLetter | Remove-PSDrive
Get-PSDrive $t_driveLetter | Remove-PSDrive