PowerShell Parameter Splatting Demystified: Essential Tips for Beginners!

Hashtables

Long PowerShell commands with tons of parameters can be a headache—especially when you’re automating tasks or managing infrastructure with scripts. In this post, we’ll explore how splatting and hashtables can simplify your scripts, reduce errors, and make your code easier to maintain.

Why Use Splatting?

When commands get long, they become harder to read, update, and troubleshoot. Splatting solves this by allowing you to pass a collection of parameters using a hashtable. This makes your scripts:

  • Cleaner and easier to read
  • More flexible for updates
  • Less error-prone

Creating a Hashtable

A hashtable is a collection of key-value pairs. Each key represents a parameter name, and each value is the value you want to assign.  The hashtable starts by declaring a variable with a dollar sign $, then an equal sign =, followed by an @ symbol and opening and closing braces or square brackets {}.  The data between the braces includes key-value pairs.

Here’s how to create a simple hashtable:

$car = @{
    Make  = "Toyota"
    Model = "Camry"
    Year  = 2022
    Color = "Blue"
}

Access individual values like this:

$car.Color  # Returns "Blue"

You can also view all key-value pairs:

$car | Format-Table
format-table output

And inspect the object type and methods with the Get-Member command:

$car | Get-Member
Get-Member Output

Want to add a new key-value pair? Use the .Add() method:

$car.Add("Price", 25000)
$car | Format-Table #View the updated table
Format-table output

Using Splatting with PowerShell Commands

Let’s say you want to create a new Azure VM. Here’s the traditional way:

New-AzVM -ResourceGroupName <ResourceGroupName> -Name <Name> -Location <Location> -VirtualNetworkName <VirtualNetworkName> -SubnetName <SubnetName> -SecurityGroupName <SecurityGroupName> -PublicIpAddressName <PublicIpAddressName> -OpenPorts <OpenPorts> -Image <Image> -Size <Size> -Credential <Credential>

Clean it Up with Splatting

Now let’s clean that up using splatting using the example from the video:

$vmParams = @{
    ResourceGroupName     = 'AASplatting'
    Name                  = 'DemoVM2'
    Location              = 'CentralUS'
    VirtualNetworkName    = 'VNet03'
    SubnetName            = 'Default'
    SecurityGroupName     = 'DemoVM2NSG'
    PublicIpAddressName   = 'myPublicIP2'
    OpenPorts             = 3389
    Image                 = 'MicrosoftWindowsServer:WindowsServer:2022-datacenter-azure-edition:latest'
    Size                  = 'Standard_B2ms'
    Credential            = $credentials
    Zone                  = '1'
}

The command is executed by using the @ symbol followed by the name of the hashtable variable. When splatting, the @ symbol replaces the dollar sign ($), indicating to PowerShell that a list of parameters is being passed rather than a single parameter.  

New-AzVM @vmParams

Reusing Splatting for Other Commands

You can use splatting for other commands too. For example, to get the VM you just created:

$newVmParams = @{
    ResourceGroupName = 'AASplatting'
    Name              = 'DemoVM2'
}
Get-AzVM @newVmParams

Final Thoughts

Splatting is a simple but powerful technique that makes your PowerShell scripts cleaner and easier to manage. By using hashtables to organize parameters, you reduce errors, improve readability, and make your automation scripts more flexible.

If you’re learning PowerShell or working in IT automation, mastering splatting is a great step toward writing professional-grade scripts.

LInks

A Beginner’s Guide to the AZ-900
https://www.udemy.com/course/beginners-guide-az-900/?referralCode=C74C266B74E837F86969

Zero to Hero with Azure Virtual Desktop
https://www.udemy.com/course/zero-to-hero-with-windows-virtual-desktop/?referralCode=B2FE49E6FCEE7A7EA8D4

Hybrid Identity with Windows AD and Azure AD
https://www.udemy.com/course/hybrid-identity-and-azure-active-directory/?referralCode=7F62C4C6FD05C73ACCC3

Windows 365 Enterprise and Intune Management
https://www.udemy.com/course/windows-365-enterprise-and-intune-management/?referralCode=4A1ED105341D0AA20D2E

PowerShell Commands
https://github.com/tsrob50/CiraltosTools

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Click Here!
Scroll to Top