Skip to content

Latest commit

 

History

History
85 lines (55 loc) · 4.28 KB

quick-create-powershell.md

File metadata and controls

85 lines (55 loc) · 4.28 KB
title description services author ms.service ms.subservice ms.topic ms.custom ms.date ms.author
Quickstart - Set & retrieve a secret from Key Vault using PowerShell
In this quickstart, learn how to create, retrieve, and delete secrets from an Azure Key Vault using Azure PowerShell.
key-vault
msmbaldwin
azure-key-vault
secrets
quickstart
mvc, devx-track-azurepowershell, mode-api
08/27/2024
mbaldwin

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 Azure 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 free account before you begin.

[!INCLUDE cloud-shell-try-it.md]

If you choose to install and use PowerShell locally, this tutorial requires Azure PowerShell module version 5.0.0 or later. Type Get-InstalledModule -Name Az to find the version. If you need to upgrade, see How to install Azure PowerShell. If you are running PowerShell locally, you also need to run Connect-AzAccount to create a connection with Azure.

Connect-AzAccount

Create a resource group

[!INCLUDE Create a resource group]

Create a key vault

[!INCLUDE Create a key vault]

Give your user account permissions to manage secrets in Key Vault

[!INCLUDE Using RBAC to provide access to a key vault]

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, run the following command and enter the value hVFkk965BuUv when prompted to convert it to a secure string:

$secretvalue = Read-Host -Prompt 'Enter the example password' -AsSecureString

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.