Bicep – deploying with PowerShell: Splatting

I wanted to share something that I’ve come across recently that has really helped make my PowerShell scripts neater and easier to read. It’s called Splatting and helps with providing parameters to a command.

I know, it’s sounds really boring, but it’s helped a lot with my scripting and is really easy to use. I’ve started deploying a lot of Bicep templates to Azure and using PowerShell in Visual Studio Code to do the deploying. This is great but when you start to have a long list of parameters to pass into your deployments it can start to look a bit messy and can be hard to read. I have been using the backtick (`) character at the end of each parameter so it makes the line continue, but they can be hard to see and forget causing errors when it comes to deploying my templates.

You can use splatting as shown below, to separate the command from the parameters.

I think this is a much nicer way to work with parameters than using the backtick character.

The syntax is as below:

Use the array syntax to provide parameter values for positional parameters, in which parameter names are not required. Use the Use the hash table syntax to provide parameter name and value pairs. You can pass some parameters using splatting and others by position or by parameter name. You can also override a splatted parameter by explicitly defining a parameter in a command as of PowerShell 7.1.

PowerShell associates each value in the collection with a command parameter. Splattered parameter values are stored in named splatting variables, which look like standard variables, but begin with the @ symbol instead of a $ sign. The @ symbol tells PowerShell that you are passing a collection of values, instead of a single value.

We’re using a hash table here which is composed of a key and a value pair. You can use this format for all parameter types, including positional and switch parameters. Positional parameters must be assigned by name. The first key is Name and the first value is exampleDeployment with the date on the end. The second key is TemplateFile and the second value is ’04 Deploy resources conditionally.bicep’. The third key is ResourceGroupName and the third value is ‘Bicep-DeployResourcesConditionally’.

You can then use the parameters in a command. The second command above uses the $Parameters variable in a command with splatting by using @Parameters.

About the author