How to Create a Directory in Azure File Share using PowerShell

Introduction 

In this blog, you will see how to create a directory in file share using PowerShell.

Prerequisites

Install Azure PowerShell Module to run the script.

PowerShell Script

Open Notepad and paste the following script. Save the file as script.ps1.

  1. ################# Azure Blob Storage - PowerShell ####################  
  2.  
  3. ## Input Parameters  
  4. $resourceGroupName="azpractice"  
  5. $storageAccName="azstorageacc1122020"  
  6. $fileShareName="azevents2020"  
  7. $directoryPath="Presentation"  
  8.  
  9. ## Connect to Azure Account  
  10. Connect-AzAccount   
  11.  
  12. ## Function to create directory  
  13. Function CreateDirectory  
  14. {  
  15.     Write-Host -ForegroundColor Green "Creating directory in file share.."    
  16.     ## Get the storage account context  
  17.     $ctx=(Get-AzStorageAccount -ResourceGroupName $resourceGroupName -Name $storageAccName).Context  
  18.     ## Create directory  
  19.     Get-AzStorageShare -Context $ctx -Name $fileShareName | New-AzStorageDirectory -Path $directoryPath  
  20. }  
  21.   
  22. CreateDirectory   
  23.  
  24. ## Disconnect from Azure Account  
  25. Disconnect-AzAccount  
Open the Windows PowerShell window and navigate to the location where the script file was saved.

Run the following command.

.\script.ps1

Result

 
 
Reference

https://docs.microsoft.com/en-us/powershell/module/az.storage/new-azstoragedirectory?view=azps-3.3.0

Summary

In this blog, you saw how to create a directory in file share using PowerShell.