Quickstart: Set and retrieve a secret from Azure Key Vault using PowerShell

Azure Key Vault is a cloud service that works as a secure secrets store. You can securely store keys, passwords, certificates, and other secrets. For more information on Key Vault, you may review the Overview. In this quickstart, you use PowerShell to create a key vault. You then store a secret in the newly created vault.

If you don't have an Azure subscription, create a trial subscription before you begin.

Note

Before you can use Azure CLI in Microsoft Azure operated by 21Vianet, please run az cloud set -n AzureChinaCloud first to change the cloud environment. If you want to switch back to Azure Public Cloud, run az cloud set -n AzureCloud again.

If you choose to install and use PowerShell locally, this tutorial requires Azure PowerShell module version 5.0.0 or later. Type Get-Module az -ListAvailable to find the version. If you need to upgrade, see Install Azure PowerShell module. If you are running PowerShell locally, you also need to run Connect-AzAccount -Environment AzureChinaCloud to create a connection with Azure.

Connect-AzAccount -Environment AzureChinaCloud

Create a resource group

A resource group is a logical container into which Azure resources are deployed and managed. Use the Azure PowerShell New-AzResourceGroup cmdlet to create a resource group named myResourceGroup in the chinaeast location.

New-AzResourceGroup -Name "myResourceGroup" -Location "ChinaEast"

Create a key vault

Use the Azure PowerShell New-AzKeyVault cmdlet to create a Key Vault in the resource group from the previous step. You will need to provide some information:

  • Key vault name: A string of 3 to 24 characters that can contain only numbers (0-9), letters (a-z, A-Z), and hyphens (-)

    Important

    Each key vault must have a unique name. Replace with the name of your key vault in the following examples.

  • Resource group name: myResourceGroup.

  • The location: China North.

New-AzKeyVault -Name "<your-unique-keyvault-name>" -ResourceGroupName "myResourceGroup" -Location "China North"

The output of this cmdlet shows properties of the newly created key vault. Take note of the two properties listed below:

  • Vault Name: The name you provided to the -Name parameter above.
  • Vault URI: In the example, this is https://<your-unique-keyvault-name>.vault.azure.cn/. Applications that use your vault through its REST API must use this URI.

At this point, your Azure account is the only one authorized to perform any operations on this new vault.

Give your user account permissions to manage secrets in Key Vault

Use the Azure PowerShell Set-AzKeyVaultAccessPolicy cmdlet to update the Key Vault access policy and grant secret permissions to your user account.

Set-AzKeyVaultAccessPolicy -VaultName "<your-unique-keyvault-name>" -UserPrincipalName "user@domain.com" -PermissionsToSecrets get,set,delete

Adding a secret to Key Vault

To add a secret to the vault, you just need to take a couple of steps. In this case, you add a password that could be used by an application. The password is called ExamplePassword and stores the value of hVFkk965BuUv in it.

First convert the value of hVFkk965BuUv to a secure string by typing:

$secretvalue = ConvertTo-SecureString "hVFkk965BuUv" -AsPlainText -Force

Then, use the Azure PowerShell Set-AzKeyVaultSecret cmdlet to create a secret in Key Vault called ExamplePassword with the value hVFkk965BuUv :

$secret = Set-AzKeyVaultSecret -VaultName "<your-unique-keyvault-name>" -Name "ExamplePassword" -SecretValue $secretvalue

Retrieve a secret from Key Vault

To view the value contained in the secret as plain text, use the Azure PowerShell Get-AzKeyVaultSecret cmdlet:

$secret = Get-AzKeyVaultSecret -VaultName "<your-unique-keyvault-name>" -Name "ExamplePassword" -AsPlainText

Now, you have created a Key Vault, stored a secret, and retrieved it.

Clean up resources

Other quickstarts and tutorials in this collection build upon this quickstart. If you plan to continue on to work with other quickstarts and tutorials, you may want to leave these resources in place.

When no longer needed, you can use the Remove-AzResourceGroup command to remove the resource group, Key Vault, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup

Next steps

In this quickstart you created a Key Vault and stored a secret in it. To learn more about Key Vault and how to integrate it with your applications, continue on to the articles below.