How to Create an Azure File Share Using PowerShell

In this blog post, we’ll show you how to create an Azure file share using PowerShell.

Azure Files is a fully managed cloud-based file-share service provided by Microsoft Azure. With Azure Files, you can easily store and share files in the cloud, and access them from anywhere using a variety of protocols, including SMB, NFS, and REST API.

Install Azure PowerShell

Install the Azure PowerShell module by running the following command:

Install-Module -Name Az -AllowClobber 

This command installs the latest version of the Azure PowerShell module, which is required to interact with Azure services.

Connect to your Azure account by running the following command and following the prompts:

Connect-AzAccount

This command opens a browser window and prompts you to sign in to your Azure account. Once you sign in, PowerShell will be authenticated with your Azure account.

Create a new resource group by running the following command:

New-AzResourceGroup -Name <ResourceGroupName> -Location <Location> 

Create a new storage account by running the following command:

New-AzStorageAccount -ResourceGroupName <ResourceGroupName> -Name <StorageAccountName> -SkuName Standard_LRS -Location <Location> 

Create a new file share by running the following command:

New-AzStorageShare -Context (New-AzStorageContext -StorageAccountName <StorageAccountName> -ResourceGroupName <ResourceGroupName>) -Name <FileShareName> 

This command creates a new file share in Azure, which is where you can store your files.

Complete Code

You can find the complete code below.

# Set Variables
$rg="files"
$storagename="storage-account-name"
$location="azure-region"
$fileshare="file-share-name"

# Set Azure subscription
Set-AzContext -SubscriptionId "add-your-sub-id"

# Create a resource group
New-AzResourceGroup -Name $rg -Location $location

# Create a new storage account
New-AzStorageAccount -ResourceGroupName $rg -Name $storagename -SkuName Standard_LRS -Location $location

# Get storage account key 
$storageKey = (Get-AzStorageAccountKey -ResourceGroupName $rg -Name $storagename).Value[0]

# Save storage key
$storageContext = New-AzStorageContext -StorageAccountName $storagename -StorageAccountKey $storageKey

# Create a new file share
New-AzStorageShare -Context $storageContext -Name $fileshare

Posted

in

,

by

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.