Powershell command to delete files in Azure fileshare

SriRaghavendran Prabhakaran 365 Reputation points
2025-03-03T09:43:14.86+00:00

Hello Team.

I have an requirement to delete the files from Azure file share exclude file created in last 24 hours and i am using the below command. it is not working as expected. can you please help?

Where-Object{$file.Properties.Last Modified -lt ((Get-Date).AddDays(-1))}
Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,382 questions
0 comments No comments
{count} votes

Accepted answer
  1. Andreas Baumgarten 118.9K Reputation points MVP
    2025-03-03T12:21:30.4666667+00:00

    Hi @SriRaghavendran Prabhakaran ,

    please give this a try:

    $date = (Get-Date).AddDays(-1)
    $allObjs = Get-AzStorageFile -Context $ctx -ShareName $sharename
    foreach ($obj in $allObjs) {
        if ($($obj.ListFileProperties.IsDirectory) -eq $false) {
            Write-Host "$($obj.Name) last modified on $($obj.LastModified.DateTime)"
            if ($($obj.LastModified.DateTime) -lt $date) {
                Write-Host "$($obj.Name) is older than $date  and will be deleted" -ForegroundColor Magenta
            }
            else {
                Write-Host "$($obj.Name) is newer than $date  and will not be deleted" -ForegroundColor Blue
            }
        }
    }
    
    

    You should get an output like this:

    User's image

    My script sample will not delete anything. You have to add this by yourself in the if block.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Andreas Baumgarten 118.9K Reputation points MVP
    2025-03-03T09:56:15.66+00:00

    Hi @SriRaghavendran Prabhakaran ,

    what is the content of $file?
    Maybe you like to post the full script with the part how the variable $file is set?

    Without knowing the content of $file you can try this:

    {$file.CloudFile.Properties.LastModified -lt (Get-Date).AddDays(-1))}
    
    

    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards

    Andreas Baumgarten

    1 person found this answer 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.