How to return file name and last modified from fileshare on azure storage

Liam 0 Reputation points
2024-02-29T20:28:28.61+00:00

I have an azure storage account with a lot of data, I want to see if any changes have been made to the documents by getting the filename and the last modified stamp. There are a lot of files and the portal doesn't allow sorting by modified date like you can with blob storage (as at 29 feb 2024) and manually reviewing data using the unc path is not an option.

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
1,420 questions
Azure Storage
Azure Storage
Globally unique resources that provide access to data management services and serve as the parent namespace for the services.
3,529 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Q&A Assist
    2024-02-29T20:28:28.6133333+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To return the filename and last modified date of files in an Azure file share, you can use Azure PowerShell or Azure CLI. Here's an example using Azure PowerShell:

    1. Install the Azure PowerShell module if you haven't already done so. You can find instructions here.
    2. Connect to your Azure account using Connect-AzAccount.
    3. Get the storage account context using Get-AzStorageAccount.
    4. Get the file share using Get-AzStorageShare.
    5. Get the files in the file share using Get-AzStorageFile.
    6. Select the properties you want to display using Select-Object.

    Here's an example PowerShell script that returns the filename and last modified date of all files in a file share:

    # Connect to your Azure account
    Connect-AzAccount
    
    # Get the storage account context
    $ctx = Get-AzStorageAccount -ResourceGroupName <resource-group-name> -Name <storage-account-name>
    
    # Get the file share
    $share = Get-AzStorageShare -Context $ctx -Name <file-share-name>
    
    # Get the files in the file share and select the properties you want to display
    Get-AzStorageFile -Context $ctx -ShareName $share.Name -Path <path-to-files> | Select-Object Name, LastModified
    

    Replace <resource-group-name>, <storage-account-name>, <file-share-name>, and <path-to-files> with the appropriate values for your environment.


    References:

    0 comments No comments

  2. Nehruji R 8,181 Reputation points Microsoft External Staff Moderator
    2024-03-01T06:57:42.1166667+00:00

    Hello Liam,

    Welcome to Microsoft Q&A Forum, Thank you for posting your query here!

    Adding more information to above answer-To determine if any changes have occurred in your Azure storage account files, including their filenames and last modified timestamps.

    Using PowerShell and Azure Storage SDK:

    • You can use the Azure.Storage.Files.Shares package to interact with Azure File shares.
    • Here’s an example using C# (similar principles apply to PowerShell):

    C#

    using Azure.Storage.Files.Shares;

    using Azure.Storage.Sas;

    using Azure.Storage.Files.Shares.Specialized;

    // Set your connection string and share name

    string connectionString = "your-connection-string";

    string shareName = "your-share-name";

    // Create a ShareServiceClient

    ShareServiceClient serviceClient = new ShareServiceClient(connectionString);

    // Get a reference to the share

    ShareClient shareClient = serviceClient.GetShareClient(shareName);

    // List files in the share

    foreach (ShareFileItem fileItem in shareClient.GetRootDirectoryClient().GetFilesAndDirectories())

    {

    if (fileItem.IsFile)
    
    {
    
        ShareFileClient fileClient = shareClient.GetFileClient(fileItem.Name);
    
        ShareFileProperties properties = fileClient.GetProperties();
    
        DateTimeOffset lastModified = properties.LastModified;
    
        Console.WriteLine($"File: {fileItem.Name}, Last Modified: {lastModified}");
    
    }
    

    }

    AI-generated code. Review and use carefully.

    • Replace your-connection-string and your-share-name with your actual values.
    • This code lists all files in the root directory of the specified share and displays their names and last modified timestamps.
    • Considerations:
    • The above example retrieves files from the root directory. If you have subdirectories, you can recursively traverse them to get all files.
    • Ensure that you have the necessary permissions to access the Azure File share.

    Similar thread for reference - https://learn.microsoft.com/en-us/answers/questions/1565053/azure-powershell-script-for-listing-azure-file-sha

    Hope this helps! Please let us know if you have any further queries. I’m happy to assist you further.

    Please 191316-screenshot-2021-12-10-121802.pngand “up-vote” wherever the information provided helps you, this can be beneficial to other community members.

    0 comments No comments

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.