Another tidbit to come from the scripts I’ve been reviewing is how messy commands with tons of parameters can be.  You end up with really long lines, uneven lines, and/or lots of backticks.

But What Can We Do?

But there are commands that require many parameters or long parameters.  We can only stuff so much in variables, and if we name them clearly, they tend to be longer too.  So how can we deal with this?

Splat ‘Em!

Version 2 of PowerShell introduces a concept called "splatting".  What that means is that I can take a hashtable with keys that are the names of parameters and use that to pass input to my function, script, or cmdlet. In order to make this magic happen, I have to change th "$" in the variable indicator to "@" when I supply that hashtable to the command.


Invoke-Command -ComputerName $MyDomainController -Credential $DomainAdminCreds -ConfigurationName ActiveDirectoryAdmin -JobName GettingWorkDone -AsJob -ScriptBlock {get-aduser -filter *}

Becomes this..

$MyCommandParameters = @{
    ComputerName = $MyDomainController
    Credential = $DomainAdminCreds
    ConfigurationName = ‘ActiveDirectoryAdmin’
    JobName = ‘GettingWorkDone’
    AsJob = $true
    ScriptBlock = {Get-ADUser -filter *}
Invoke-Command @MyCommandParameters

Much more readable (to me at least)!