A simple but useful trick. Instead of specifying parameters and arguments to a cmdlet on the same line as the cmdlet, you can put these into a hash-table and “splat” the table to the cmdlet. Here’s an example.
Old way:
1 |
PS> Write-Host -ForegroundColor Green -BackgroundColor Black "Some Text" |
New way:
1 2 3 4 |
# Create a hash-table PS> $colors = @{ ForegroundColor = "Green"; BackgroundColor = "Black" } # Splat this hash-table and pass to Write-Host PS> Write-Host @colors "Some Text" |
Notice you pass the variable $colors
with a @
sign instead of the usual $
sign. That’s what splats the variable. The hash-table is broken and the keys treated as parameter names and values treated as arguments.
You can use this trick with any cmdlet.