r/PowerShell • • Apr 05 '19

PowerShell tricks: Splatting

https://blog.destruktive.one/powershell-tricks-splatting/
150 Upvotes

58 comments sorted by

View all comments

4

u/[deleted] Apr 05 '19

wow, that's interesting. so the takeaway is that you can pass an array to a cmdlet with the parameters. TIL. thnx

7

u/DrSinistar Apr 05 '19

pass a hashtable to a cmdlet

FTFY. They're different types. It's best not to confuse them.

5

u/TheIncorrigible1 Apr 05 '19

The article does a terrible job of explaining the feature; even the documentation is a better read.

You can pass arrays to splat a string array to external utilities (such as batch scripts, executables, etc.)

2

u/[deleted] Apr 05 '19

[removed] — view removed comment

3

u/TheIncorrigible1 Apr 05 '19

Yes, actual splatting. It's a neater way than writing out a long list of arguments; just like hashtable splatting for cmdlets.

2

u/[deleted] Apr 05 '19

[removed] — view removed comment

3

u/Lee_Dailey [grin] Apr 05 '19

howdy Szeraax,

for me, it is building an array of options for robocopy & then using that as the argument list for the util. it apparently works very much like that for other arguments. something like this ...

$RC_Params = @(
    $Source
    $Destination
    $FileSpec 
    # put your current options below
    "/Log:$FullLogFileName"
    '/E'
    '/TEE'
    )

robocopy $RC_Params

it aint what i consider splatting, but it fits well enuf & is documented as such ... but i can't find the silly docs for it right now. [blush]

take care,
lee

4

u/TheIncorrigible1 Apr 05 '19

That's not splatting. You're missing the syntax.

robocopy @RC_Params

4

u/ka-splam Apr 05 '19

What difference does it make?

PS C:\> $x=@('echo','b','c')
PS C:\> cmd /c @x
b c
PS C:\> cmd /c $x
b c

4

u/TheIncorrigible1 Apr 05 '19

How it passes the arguments. $x will convert your array using ToString and doesn't pass them properly.

→ More replies (0)

2

u/Lee_Dailey [grin] Apr 05 '19

howdy TheIncorrigible1,

as i mentioned - it aint what i consider splatting. [grin] it's all positional or /ArgName:Value stuff. i had not tried it with an @ instead of a $ ... [blush]

take care,
lee

2

u/TheIncorrigible1 Apr 05 '19
$params = @( '/all' )
ipconfig @params