Rename Azure Storage Blob using PowerShell

At the time of writing this there is no API to rename an Azure Storage blob in one operation. You have to copy the blob and delete the original one after the copy process completes.

You can vote for the feature here: Rename blobs without needing to copy them

Until then you can use my convenience Rename-AzureStorageBlob cmdlet:


function Rename-AzureStorageBlob
{
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
        [Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageBlob]$Blob,

        [Parameter(Mandatory=$true, Position=1)]
        [string]$NewName
    )

  Process {
    $blobCopyAction = Start-AzureStorageBlobCopy `
        -ICloudBlob $Blob.ICloudBlob `
        -DestBlob $NewName `
        -Context $Blob.Context `
        -DestContainer $Blob.ICloudBlob.Container.Name

    $status = $blobCopyAction | Get-AzureStorageBlobCopyState 

    while ($status.Status -ne 'Success')
    {
        $status = $blobCopyAction | Get-AzureStorageBlobCopyState
        Start-Sleep -Milliseconds 50
    }

    $Blob | Remove-AzureStorageBlob -Force
  }
}

It accepts the blob as pipeline input so you can pipe the result of the Get-AzureStorageBlob to it and just provide a new name:

$connectionString= 'DefaultEndpointsProtocol=https;AccountName....'
$storageContext = New-AzureStorageContext -ConnectionString $connectionString

Get-AzureStorageBlob -Container 'MyContainer' -Context $storageContext -Blob 'myBlob.txt'|
    Rename-AzureStorageBlob -NewName 'MyNewBlob.txt'

You can also download the script from my GitHub repository.

4 thoughts on “Rename Azure Storage Blob using PowerShell”

  1. An AZ variant… not sure what the type should be of the first parameter, but Object also works:

    function Rename-AzStorageBlob
    {
    [CmdletBinding()]
    Param
    (
    [Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)]
    [Object]$Blob,
    [Parameter(Mandatory=$true, Position=1)]
    [string]$NewName
    )

    Process {
    $blobCopyAction = Start-AzStorageBlobCopy `
    -ICloudBlob $Blob.ICloudBlob `
    -DestBlob $NewName `
    -Context $Blob.Context `
    -DestContainer $Blob.ICloudBlob.Container.Name

    $status = $blobCopyAction | Get-AzStorageBlobCopyState

    while ($status.Status -ne ‘Success’)
    {
    $status = $blobCopyAction | Get-AzStorageBlobCopyState
    Start-Sleep -Milliseconds 50
    }

    $Blob | Remove-AzStorageBlob -Force
    }
    }

    Like

  2. Martin,

    Thanks for posting this! Do you mind if I post an edited version of this script on my blog? I have edited it to work for Azure Files. I will give you credit and link to this page.

    Thank you,
    Jason

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s