This repository provides a PowerShell script to delete files and directories from an Azure FileShare using the Azure Storage REST API. Unlike access key–based approache, this method leverages direct REST calls for fine-grained control and can be integrated into automation pipelines or restricted environments where SDKs/CLI tools are not available.
- The script acquires a Bearer token using either Managed Identity or user login (Connect-AzAccount).
- Recursively traverses directories until all eligible files are processed.
- It uses Azure Storage REST API (Invoke-RestMethod and Invoke-WebRequest) to list and delete files.
- Files are deleted if their Last-Modified timestamp is older than the cutoff.
- No Az.Storage or Az.Files modules are required — only Az.Accounts for token acquisition.
- Lightweight and dependency-free (no Az PowerShell modules required).
- Ideal for automation jobs, restricted environments, or custom integrations.
Before running the script, ensure you have:
- Proper network access to the Azure FileShare endpoint.
- (https://.file.core.windows.net/...). The Automation Account must be able to reach this endpoint.
- Azure Automation Account with PowerShell 7.2 runtime
- System-assigned managed identity enabled for the Automation Account
- Managed identity assigned the following role on the storage account:
Storage File Data Privileged Contributor
- Az.Accounts module must be available in the Automation Account runtime (it is included by default in 7.2).
- If you set
$useManagedIdentity = $false, then the script will use the logged-in user’s identity. In that case, the user must also have the sameStorage File Data Privileged Contributorrole on the storage account.
The script defines the following parameters:
| Parameter | Description | Example Value |
|---|---|---|
storageAccount |
Name of the storage account | stsamaks8dsc |
fileShare |
Name of the file share | fslogix |
cutoffHours |
Number of hours; files older than this will be deleted | 24 |
useManagedIdentity |
Use managed identity or logged in user's identity (interactive runs) | $true |
- Import the script into your Automation Account as a PowerShell runbook.
- Configure the runbook to use PowerShell 7.2 runtime.
- Ensure the Automation Account’s managed identity has the required roles.
- Set up a schedule to run the runbook daily (or at your desired frequency).
Please read CONTRIBUTING.md for guidelines on how to contribute.
We expect all contributors to follow our Code of Conduct.
Created and maintained by Handover2AI-byExistence.
If you find this useful, feel free to star ⭐ the repo or open issues for improvements.