Store a multi-line secret in Azure Key Vault

The Azure CLI quickstart and Azure PowerShell quickstart demonstrate how to store a single-line secret. You can also use Key Vault to store a multi-line secret, such as a JSON file or RSA private key.

Multi-line secrets cannot be passed to the Azure CLI az keyvault secret set command or the Azure PowerShell Set-AzKeyVaultSecret cmdlet through the commandline. Instead, you must first store the multi-line secret as a text file.

For example, you could create a text file called "secretfile.txt" containing the following lines:

This is my
multi-line
secret

You can then pass this file to the Azure CLI az keyvault secret set command using the --file parameter.

az keyvault secret set --vault-name "<your-unique-keyvault-name>" --name "MultilineSecret" --file "secretfile.txt"

With Azure PowerShell, you must first read in the file using the Get-Content cmdlet, then convert it to a secure string using ConvertTo-SecureString.

$RawSecret =  Get-Content "secretfile.txt" -Raw
$SecureSecret = ConvertTo-SecureString -String $RawSecret -AsPlainText -Force

Lastly, you store the secret using the Set-AzKeyVaultSecret cmdlet.

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

In either case, you can then view the stored secret using the Azure CLI az keyvault secret show command or the Azure PowerShell Get-AzKeyVaultSecret cmdlet.

az keyvault secret show --name "MultilineSecret" --vault-name "<your-unique-keyvault-name>" --query "value"

The secret will be returned with newlines embedded:

"This is\nmy multi-line\nsecret"

Next steps