Skip to main content

Reading Bicep parameter files with PowerShell

· 5 min read
Hasan Gural

Bicep parameter files allow you to define values in an individual file that are then passed to your main Bicep Templates file. The parameter file exposes values that may change from a given subscription, environment, and/or region. Leveraging a parameter file drives consistency in your IaC deployments while providing flexibility. For example, an organization can use these files to right-size nonproduction environments to save costs while maintaining the same core infrastructure across all deployments.

Reading Bicep Parameter Files with PowerShell

In addition, these parameter files streamline the CI/CD deployment process. Since each parameter file is under source control and passed into the appropriate automated deployment steps, they ensure a consistent and repeatable deployment experience. In this article, we will explore how to create, read, and use a Bicep parameters file via PowerShell.

Overview of Bicep Parameter Files

Bicep parameter files come in two flavors: files with a .bicepparam extension and JSON parameter files. These files are used to supply parameter values to your Bicep templates, which define your Azure infrastructure. A single Bicep template file can have multiple associated parameter files (e.g., main.dev.bicepparam for development and main.prod.bicepparam for production), allowing you to deploy the same infrastructure in multiple environments with different configurations.

Reading a Bicep Parameter File with PowerShell

Now that we understand what Bicep parameter files are and how they work, let's focus on how you can read them using PowerShell. The key question is - Why would you want to read a Bicep parameter file with PowerShell? The answer lies in the need to extract and use the parameter values in your automation scripts, deployment pipelines, or configuration files so that you can drive further automation based on these values. Some of you may be wondering you can read the required values directly from deployment outputs, but there are scenarios where you need to read the parameter files directly. For example, you may want to generate a configuration file that drives further automation steps or understand the resource configuration before deployment you might need to prepare some resources before deployment.

The following example demonstrates how to:

  1. Build the parameters file using the Bicep CLI.
  2. Parse the JSON output into a PowerShell object.
  3. Extract key parameters for further use.

Example Parameter file: main.dev.bicepparam


using './main.bicep'

param orgName = 'myorg'
param location = 'eastus'
param domainName = 'contoso'
param envName = 'dev'

Step 1: Build the Parameters File

Use the Bicep CLI command bicep build-params to generate a JSON representation of your parameter file. I recommend redirecting the error output to $null to suppress any unnecessary messages.


$ParamFilePath = "C:\path\to\your\main.dev.bicepparam"
$output = bicep build-params $ParamFilePath --stdout 2>$null

Here, $ParamFilePath is the full path to your .bicepparam file. I would expect to see the $output variable contain a JSON string similar to the following:

{
"$schema": "https://schema.management.azure.com/schemas/2019-04-01/deploymentParameters.json#",
"contentVersion": "1.0.0.0",
"parameters": {
"orgName": {
"value": "myorg"
},
"location": {
"value": "eastus"
},
"domainName": {
"value": "contoso"
},
"envName": {
"value": "dev"
}
}
}

Step 2: Parse the JSON Output

The output from the above command is in JSON format. Convert it into a PowerShell object using ConvertFrom-Json. Often, the JSON contains a property called parametersJson, which itself is a JSON string containing the parameter values. We must parse that nested JSON as well.

$paramContent = ConvertFrom-Json $output
$paramJson = $paramContent.parametersJson | ConvertFrom-Json

After this step, $paramJson contains a structured object with all of the parameters defined in your file.

Step 3: Extract values from the Parameter Object

Parameter file defines parameters like orgName, location, domainName, and envName. We can extract these values as follows:

# Define a hashtable for mapping location names to their short codes

$locationShortCodes = @{
"eastus" = "EUS"
"westus" = "WUS"
"centralus" = "CUS"
}


$orgName = $paramJson.parameters.orgName.value
$locationShortCode = $locationShortCodes[$paramJson.parameters.location.value]
$domainName = $paramJson.parameters.domainName.value
$envName = $paramJson.parameters.envName.value

Write-Output "Organization: $orgName"
Write-Output "Location Short Code: $locationShortCode"
Write-Output "Domain Name: $domainName"
Write-Output "Environment Name: $envName"

Expected Output:


Write-Output "Organization: $orgName"
Write-Output "Location Short Code: $locationShortCode"
Write-Output "Domain Name: $domainName"
Write-Output "Environment Name: $envName"
Organization: myorg
Location Short Code: EUS
Domain Name: contoso
Environment Name: dev

Generating a Deployment Configuration File

Once you have extracted the necessary parameters from Bicep parameter file, you can use them to generate configuration files that drive further automation in your deployment pipelines. For instance, by reading environment-specific values, you might automatically raise a request or change request ticket in your governance platform—such as ServiceNow—so that any deployment updates are properly tracked.

This process helps ensure that your environment details, such as organization name, deployment region, domain, and environment identifier, are consistently applied across your infrastructure. It also allows you to enforce governance policies and streamline the change request process.

Conclusion

Bicep parameter files are a powerful way to manage the configuration of your Azure infrastructure deployments. In this guide, we explored how to read a Bicep parameter file using the PowerShell.

Stay tuned for more articles on Bicep, PowerShell, and DevOps automation.