PowerShell Genie

Use splatting for a CIM query

Script 2 min read Windows PowerShell 5.1 and PowerShell 7

Documentation: Microsoft Learn

A hashtable holds parameter names as keys with their values attached, then @params in place of the individual parameters hands the whole set to Get-CimInstance.

The script

1PS> $Computer = "SRV01"
2PS> $params =@{ComputerName= $Computer;ClassName= "Win32_OperatingSystem" }; Get-CimInstance @params

Script reader

Inspect before you run

Inspection only

Inspect the script below. It does not run in this browser.

Inspection only — no command has run.

    Show reference output
    1 SystemDirectory : C:\Windows\system32
    2 Organization : Contoso
    3 BuildNumber : 26100
    4 Caption : Microsoft Windows 11 Pro
    5 Version : 10.0.26100
    6 CSName : WS01
    7 LastBootUpTime : 9/8/2026 6:15:04 AM
    Inspect this example’s structure

    Read the command and its supplied arguments. This example requires real PowerShell or its module; this diagram does not execute it.

    $Computer = "SRV01" $params =@{ComputerName= $Computer;ClassName= "Win32_OperatingSystem" }; Get-CimInstance @params
    1. Stage 1$ComputerPositional values or no named parameters

      How it works

      A hashtable holds parameter names as keys with their values attached, then @params in place of the individual parameters hands the whole set to Get-CimInstance. Using the at-sign rather than a dollar sign is what tells PowerShell to unroll the hashtable into arguments.

      Adapting it

      Add keys conditionally with an if statement before the call, which is how -ComputerName gets included only when a remote target was actually supplied.

      If the result is unexpected

      A splatted CIM query fails binding or targets the wrong computer.

      Hashtable keys must match command parameters and the target variable may be unset or hold an unintended value.

      1. Inspect the splat keys and target value, then compare them with Get-CimInstance help.

      Validate the result

      The query binds and returns the expected class from the intended target.

      Keep the check reliable

      Build command-specific splats and validate required target values before invocation.

      Primary documentation

      Get-CimInstance reference

      Continue
      Genie guideUse splatting for a CIM query

      A hashtable holds parameter names as keys with their values attached, then @params in place of the individual parameters hands the whole set to Get-CimInstance.