How to upload local directory with its subdirectories to Azure fileshare using powershell

MoTaar 310 Reputation points
2024-01-05T00:04:20.05+00:00

I am trying to upload local directory with its subdirectories to Azure fileshare using powershell, but I not succeeding in it I tried az copy:

azcopy copy [$destinationPath]  [$sasToken] --recursive --preserve-smb-permissions=true --preserve-smb-info=true

But I am getting this error:

failed to parse user input due to error: the inferred source/destination combination could not be identified, or is currently not supported

Is there a solution for this or is there another way to do it?

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,358 questions
PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,809 questions
{count} votes

Accepted answer
  1. Sedat SALMAN 14,150 Reputation points MVP
    2024-01-05T00:10:34.61+00:00

    please check the following may help you

    $resourceGroupName = "xxxx"
    $storageAccName = "yyyy"
    $fileShareName = "tttt"
    $localDirectory = "C:\path\" 
    
    Connect-AzAccount
    
    # Get the storage account context
    $ctx = (Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
    
    $fileShare = Get-AZStorageShare -Context $ctx -Name $fileShareName
    
    Function UploadFilesRecursively {
        param (
            [string]$sourceDir,
            [string]$destDir
        )
        
        # Create the destination directory in the file share
        New-AzStorageDirectory -Context $ctx -ShareName $fileShareName -Path $destDir -ErrorAction SilentlyContinue
        
        # Upload files in the current directory
        Get-ChildItem -Path $sourceDir -File | ForEach-Object {
            $localFilePath = $_.FullName
            $destFilePath = "$destDir/$($_.Name)"
            Set-AzStorageFileContent -ShareName $fileShareName -Source $localFilePath -Path $destFilePath -Context $ctx
            Write-Host "Uploaded: $localFilePath to $destFilePath"
        }
    
        # Recurse into subdirectories
        Get-ChildItem -Path $sourceDir -Directory | ForEach-Object {
            $subDirSource = $_.FullName
            $subDirDest = "$destDir/$($_.Name)"
            UploadFilesRecursively -sourceDir $subDirSource -destDir $subDirDest
        }
    }
    
    
    $rootDirName = Split-Path -Path $localDirectory -Leaf
    UploadFilesRecursively -sourceDir $localDirectory -destDir $rootDirName
    
    # Disconnect from Azure Account
    Disconnect-AzAccount
    
    
    

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.