This blog article shows you how to filter the files from Azure Storage by Last Modified date. The snippet of the PowerShell as follows.
Connect-AzAccount -Tenant TenantId -Subscription SubscriptionId
$resourceGroupName =” resourceGroupName “
$storageAccName =” storageAccName “
$directoryPath =”document”
$container = “container”
$ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
Get-AzStorageBlob -Container “document” -Context $ctx | Where-Object {$_.LastModified -gt “2024-03-15”}
Certainly! Let’s break down the PowerShell script step by step:
1. Connect-AzAccount -Tenant TenantId -Subscription SubscriptionId
:
- This command connects to your Azure account using the specified tenant ID and subscription ID.
- It authenticates you to perform operations against your Azure resources.
2. Variable Assignments:
$resourceGroupName
: This variable holds the name of the Azure resource group where your storage account resides.$storageAccName
: This variable stores the name of your Azure storage account.$directoryPath
: Specifies the directory path within the storage account container.$container
: The name of the storage container you want to work with.
3. $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context
:
- This line retrieves the storage account context (connection information) for the specified resource group and storage account.
- The context is necessary for subsequent operations on the storage account.
4. Get-AzStorageBlob -Container "document" -Context $ctx | Where-Object {$_.LastModified -gt "2024-03-15"}
:
- This command retrieves a list of blobs (files) from the specified container named “document” within the storage account.
- The
-Context $ctx
parameter ensures that the command uses the previously obtained storage account context. - The
Where-Object
filter is applied to select only blobs where theLastModified
timestamp is greater than March 15, 2024.
In summary, this script connects to your Azure account, retrieves the storage account context, and then lists the blobs in the “document” container that were modified after March 15, 2024. If any blobs meet the criteria, they will be displayed in the output12
Also: Start and Stop Azure App Service using PowerShell
You can see the return result sample as above.
Source code download: https://github.com/chanmmn/powershell/tree/main/2024/AzureStorageFilterDate?WT.mc_id=DP-MVP-36769