Azure file shares allow you to host a plain ol' Server Message Block (SMB) share in the cloud, and by using PowerShell, you can eliminate all the pointing and clicking in the Azure portal.

Since an Azure file share needs a place to store your files, you'll need to have a storage account created ahead of time. Also, I'm assuming you have downloaded and installed the AzureRm PowerShell modules, and you're authenticated to Azure in your PowerShell console.

To get started, you'll first need to create an Azure storage context object. This object lets you pass the storage account name and key to create the file share. The command to do this is called New-AzureStorageContext, and it requires you to know the storage account name you'll be using for the file share and the storage account key. The name is straightforward, but if you don't know your key, you can use the Get-AzureRmStorageAccountKey command.

Below you can see that this command returns my primary and secondary keys. I just need the primary.

PS> Get-AzureRmStorageAccountKey -ResourceGroupName General -Name generaldiag805

KeyName Value                      Permissions
------- -----                      -----------

I can retrieve the primary key and assign it to a variable by capturing the first key returned.

PS> $key = (Get-AzureRmStorageAccountKey -ResourceGroupName General -Name generaldiag805)[0].value

Once I know the storage account key and name, I can pass these to New-AzureStorageContext.

$parameters = @{
    StorageAccountName = 'generaldiag805'
    StorageAccountKey = $key
$storageContext = New-AzureStorageContext @parameters

At this point, I can create the file share or a storage share like the PowerShell command New-AzureStorageShare calls it.

New-AzureStorageShare logs -Context $storageContext

When this command is complete, it will return the endpoint representing the file share, which will be important to mount this share later.

Creating the Azure file share

Creating the Azure file share

After creating the file share, you will then notice it inside the Azure portal under Storage Accounts –> Files as shown below.

Viewing the file share within the Azure portal

Viewing the file share within the Azure portal

You now have an Azure file share created, but unless you know how to access it, it won't do much good. Since the file share is a standard SMB file share, we can actually mount this file share in Windows as a standard drive using New-PSDrive. We simply need to know the file endpoint.

We can mount the file share as a drive by creating a PSCredential object and use the New-PSDrive command specifying the Root parameter as a UNC path to the file endpoint as shown below.

$parameters = @{
    String = '<password>'
    AsPlainText = $true
    Force = $true
$acctKey = ConvertTo-SecureString @parameters

$credential = New-Object System.Management.Automation.PSCredential  ArgumentList 'Azure\fileshare',$acctKey
New-PSDrive -Name P -PSProvider Filesystem -Root '\\\logs' -Credential $credential

After mounting the drive, you can then browse the P: drive just as any other Windows drive, but you'll be working with files in Azure instead.

Subscribe to 4sysops newsletter!

An Azure file share is a great way to migrate your legacy, on-prem file shares to Azure. You don't have to worry about blob storage at all!

1 Comment
  1. Bo Geitz 4 years ago

    Great write-up. Thanks for sharing!

    Don’t forget – the new Azure Az Module for Windows PowerShell, Core, and Cloud Shell Replaces AzureRM.

Leave a reply

Your email address will not be published.


© 4sysops 2006 - 2023


Please ask IT administration questions in the forums. Any other messages are welcome.


Log in with your credentials


Forgot your details?

Create Account