Delete old snapshots on Azure Files using PowerShell

Michał Pawlicki 86 Reputation points

Looks like there's still no automatic system to remove old snapshots.

I've been trying to use the method published in azure-fileshare-snapshot-powershell-deletion

but that doesn't quite work for me.

My code is simple:

#set the contect using the connection string 
$Context = New-AzStorageContext -ConnectionString "xyz"

# Generate Snapshot List
$SnapshotList = Get-AzStorageShare -Context $context | Where-Object {$_.Name -eq $ShareName -and $_.IsSnapshot -eq $true -and $_.SnapshotTime -lt ([datetime]::UtcNow.AddDays(-4))}

# Delete Snapshots older than 4 days
foreach ($Snapshot in $SnapshotList)
       Remove-AzStorageShare -Share $Snapshot -Verbose -Force

However, I'm getting

Remove-AzStorageShare : Cannot bind parameter 'Share'. Cannot convert the "Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureS
torageFileShare" value of type "Microsoft.WindowsAzure.Commands.Common.Storage.ResourceModel.AzureStorageFileShare" to type "Microsoft.Azure.S
At line:3 char:37
+        Remove-AzStorageShare -Share $Snapshot -Verbose -Force

Is there any way to grab snapshots in the type of CloudFileShare?



Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,176 questions
{count} votes

Accepted answer
  1. deherman-MSFT 33,941 Reputation points Microsoft Employee

    @Michał Pawlicki
    Thanks for your patience while we looked into this issue. There is a change on File cmdlets from Az 4.1.0. The change is: the File Share cmdlets output changes from “CloudFileShare” to “AzureStorageFileShare”, the original output will become a child property of the new output. To resolve this you can make a simple change in your foreach loop:

     foreach ($Snapshot in $SnapshotList)  
            Remove-AzStorageShare -Share $Snapshot.CloudFileShare -Verbose -Force  

    Hope this helps! Let us know if you have any further questions.


    Please don’t forget to "Accept the answer” and “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful