How to Create an Azure File Share with PowerShell

How to Create an Azure File Share with PowerShell

One of the nice things about having a simple Windows file share on a local file server is that the support to transfer files to/from it is built into Windows. Since the share is created for SMB, you can map Windows drives to it, navigate to it’s UNC path and so on. When the Microsoft Azure and other cloud offerings first came around, storage options were greatly increased allowing you to store files in cloud storage accounts. But, it wasn’t near as easy as using an on-prem SMB share.

With Azure File Share, transferring files to/from an Azure storage account has gotten a lot easier. Azure File Share allows you to create the same kind of SMB share that you’re used to but it’s backed by Azure storage rather than local storage.

Let’s see how it can be set up but first, ensure you have an Azure subscription, have the AzureRm PowerShell module downloaded and installed by running Install-Module AzureRm and have authenticated with a user with appropriate rights by running Connect-AzureRmAccount in your PowerShell console.

Getting Started

First, I’m going to create a resource group to keep everything I’ll be creating during this article contained. I can create a new resource group called IpswitchDemo using the command New-AzureRmResourceGroup -Name IpswitchDemo -Location 'East US'.

Once you have a resource group created, you’ll then need a storage account to transfer files to and from. You can use an existing one or if you don’t already have one, the storage account can always be created using the New-StorageAccount command. I’ll go ahead and create a storage account called IpswitchDemo and place it inside the resource group I just created.

>PS> New-AzureRmStorageAccount -Name 'ipswitchdemo' -ResourceGroupName 'IpswitchDemo' -Location 'East US' -Type 'Standard_LRS'

Creating a File Share

Now that I have a storage account created, I can now create the file share. To do that, I’ll first need to assign the storage account output to a variable using Get-AzureRmStorageAccount.

PS> $storageAccount = Get-AzureRmStorageAccount -ResourceGroupName 'IpswitchDemo' -Name 'ipswitchdemo'

Next, I’ll need to get the storage account’s context object. To do that, I’ll get the storage key for our storage account and then finally assign the storage context to a variable so I can use it when creating the file share.

PS> $storageKey = (Get-AzureRmStorageAccountKey -ResourceGroupName $storageAccount.ResourceGroupName -Name $storageAccount.StorageAccountName | select -first 1).Value
PS> $storageContext = New-AzureStorageContext -StorageAccountName $storageAccount.StorageAccountName -StorageAccountKey $storageKey

At this point, we’re finally ready to create the file share. We create the Azure File Share by using the New-AzureStorageShare command passing it the name of the share to create and then finally the storage context we just obtained.

PS> New-AzureStorageShare -Name ipswitchfileshare -Context $storageContext

   File End Point:

Name               LastModified         IsSnapshot        SnapshotTime
----                ------------        ----------        ------------
ipswitchfileshare    9/22/18 3:57:49 PM +00:00   False

If no errors are received, and you see output like above, then you’ve successfully created an Azure File Share! At this point, we could even go a step further and map our file share to a local drive letter.

Creating a Local Drive

To create a local drive, we’ll need to gather up our Azure credential using the storage key for the storage account that was created and using that as the password to a user account called Azure\ipswitchdemo

PS> $secKey = ConvertTo-SecureString -String $storageKey -AsPlainText -Force
PS> $credential = New-Object System.Management.Automation.PSCredential -ArgumentList "Azure\$($storageAccount.StorageAccountName)", $secKey

Once I create the object is when I can now create the PowerShell drive. By providing the UNC path as the value for Root, using the PSCredential object just created and using the optional Persist parameter, I can create the PowerShell drive as shown below.

New-PSDrive -Name X -PSProvider FileSystem -Root "\\$($storageAccount.StorageAccountName)\ipswitchfileshare" -Credential $credential -Persist

If all went well, you should now see a new drive letter X available to you.


Comments are disabled in preview mode.
Loading animation