Find files or directory using PowerShell in both File Share and Blob Storage?

EnterpriseArchitect 3,926 Reputation points
2023-11-17T05:06:03.0733333+00:00

Folks,

I wonder how can I use the PowerShell script to find objects (Files or directories) in my specific Azure Storage Account called 'ArchiveRepo' under both

File Share https://learn.microsoft.com/en-us/azure/storage/files/storage-how-to-use-files-portal?tabs=azure-powershell and Blob Storage https://learn.microsoft.com/en-us/azure/storage/blobs/blob-containers-powershell ?

Thank you in advance.

Azure Storage Explorer
Azure Storage Explorer
An Azure tool that is used to manage cloud storage resources on Windows, macOS, and Linux.
207 questions
Azure Storage Accounts
Azure Storage Accounts
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
2,314 questions
Azure Archive Storage
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
2,100 questions
Microsoft Graph
Microsoft Graph
A Microsoft programmability model that exposes REST APIs and client libraries to access data on Microsoft 365 services.
9,183 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Carlos Solís Salazar 15,171 Reputation points
    2023-11-17T21:31:12.53+00:00

    Certainly! To find files or directories in both Azure File Share and Azure Blob Storage using PowerShell, you can follow these steps. Note that the process involves different cmdlets for File Shares and Blob Storage.

    Finding Objects in Azure File Share

    1. Install Azure PowerShell Module: If you haven't already, install the Azure PowerShell module using the Install-Module cmdlet:
         Install-Module -Name Az -AllowClobber -Scope CurrentUser
      
    2. Connect to Azure Account: Authenticate your Azure account with:
         Connect-AzAccount
      
    3. Retrieve the File Share: Use Get-AzStorageShare to get the file share. Replace <StorageAccountName> with your Storage Account name:
         $context = (Get-AzStorageAccount -ResourceGroupName "<ResourceGroupName>" -Name "<StorageAccountName>").Context
         $fileShare = Get-AzStorageShare -Context $context | Where-Object { $_.Name -eq "ArchiveRepo" }
      
    4. List Files and Directories: Enumerate files and directories in the file share:
         Get-AzStorageFile -Context $fileShare.Context -Share $fileShare.Name
      

    Finding Objects in Azure Blob Storage

    1. Retrieve the Storage Account Context: You need the context of your storage account:
         $context = (Get-AzStorageAccount -ResourceGroupName "<ResourceGroupName>" -Name "<StorageAccountName>").Context
      
    2. List Blobs in a Container: Assuming 'ArchiveRepo' is the name of your blob container:
         Get-AzStorageBlob -Container "ArchiveRepo" -Context $context
      

    Replace <ResourceGroupName> and <StorageAccountName> with your actual resource group name and storage account name. This script will list all files and directories in the specified file share and blobs in the blob container.

    For more detailed information on these cmdlets and their usage, you can refer to the official Azure documentation:

    Accept the answer if the information helped you. This will help us and others in the community as well.

    0 comments No comments

  2. Anand Prakash Yadav 1,075 Reputation points Microsoft Vendor
    2023-11-20T11:08:22.46+00:00

    Hello EnterpriseArchitect,

    Thank you for posting your query here!

    I understand you want to search for specific objects within your Azure Storage Account (both File Share and Blob Storage) based on certain criteria.

    I attempted to reproduce the inquiry and did not encounter any issues.

    Hope this helps.

    You can connect to Azure PowerShell in the Azure Cloud Shell environment.

    Below is a PowerShell script that demonstrates searching for objects containing a specific keyword in their names:

    # Specify your Azure Storage Account details
    $storageAccountName = "YourStorageAccountName"
    $resourceGroupName = "YourResourceGroupName"
    $storageAccountKey = "YourStorageAccountAccessKey"
    
    # Specify the keyword you want to search for
    $searchKeyword = "yourKeyword"
    
    # Create storage account context
    $context = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageAccountKey
    
    # Search for files in File Shares
    $fileShares = Get-AzStorageShare -Context $context
    
    foreach ($fileShare in $fileShares) {
        $filesInShare = Get-AzStorageFile -ShareName $fileShare.Name -Context $context | Where-Object { $_.Name -like "*$searchKeyword*" }
    
        foreach ($file in $filesInShare) {
            Write-Output "File Share: $($fileShare.Name), File: $($file.Name)"
        }
    }
    
    # Search for blobs in Containers
    $containers = Get-AzStorageContainer -Context $context
    
    foreach ($container in $containers) {
        $blobs = Get-AzStorageBlob -Container $container.Name -Context $context | Where-Object { $_.Name -like "*$searchKeyword*" }
    
        foreach ($blob in $blobs) {
            Write-Output "Blob Container: $($container.Name), Blob: $($blob.Name)"
        }
    }
    

    Please make sure to replace "YourStorageAccountName", "YourResourceGroupName", and "YourStorageAccountKey" with your actual storage account details and set "yourKeyword" to the keyword you are searching for. The script will then output information about the objects that match the specified keyword in their names.

    Kindly let us know if you have any further queries. I’m happy to assist you further.


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