Use splatting for a CIM query
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
Script reader
Inspect before you run
Inspect the script below. It does not run in this browser.
Inspection only — no command has run.
Show reference output
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- Stage 1$Computer
Positional 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.
- 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.