The short story about splatting is that you can use it to pass parameters to cmdlets, instead of the traditional syntax. On most cases, the usual way of working with cmdlets is fine but when you start to get a really complicated line it can get pretty annoying to keep track of.

When developing scripts for automation, you don’t want to make it look messy. Splatting will help you sort out parameters without using back ticks. Example; if we want to create a new virtual machine in Azure, we would end up with something like this:

New-AzVM -Name "Machine01" -ResourceGroupName "ResourceGroup01" -Location "westeurope" -VirtualNetworkName "VirtualNetwork01" -SubnetName "Subnet01" -SecurityGroupName "SecurityGroup01" -PublicIpAddressName "PublicIP01" -OpenPorts "3389"

How does that look? Not great, right? If that was in the middle of the script, I bet most people would have a hard time looking through that syntax. Let us look at the same example but using hash table splatting.

$VMconfig = @{
   Name = "Machine01"
   ResourceGroupName = "ResourceGroup01"
   Location = "westeurope"
   VirtualNetworkName = "VirtualNetwork01"
   SubnetName = "Subnet01"
   SecurityGroupName = "SecurityGroup01"
   PublicIpAddressName = "PublicIP01"
   OpenPorts = "3389"
}
New-AzVm @VMconfig

Much easier to read, no? Anyone reading through your scripts will thank you.

One caveat to consider, you want to use an @ with splatting. The reason is that this will make sure that PowerShell knows that you are passing several values to it and not only one. A mistake I first did was to treat it like a variable and I just couldn’t understand why it didn’t work.

This is a very rudimentary introduction to splatting. To learn more about splatting, visit docs.microsoft.com for more information.