Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerShell splatting not working

Tags:

powershell

I believe I'm missing something obvious, or misunderstanding the splatting feature of PowerShell.

I am using a hash table to pass arguments to a custom function, but it doesn't seem to take the arguments on even a simple example.

File: Test-Splat.ps1

function Test-Splat
{
    param(
        [Parameter(Mandatory=$true)][string]$Name,
        [Parameter(Mandatory=$true)][string]$Greeting
    )
    $s = "$Greeting, $Name"
    Write-Host $s
}

Then attempting to execute this with splatting, asks for a value for the second parameter.

. .\Test-Splat.ps1
$Params = @{
    Name = "Frank"
    Greeting = "Hello"
}
Test-Splat $Params

Produces the following result

cmdlet Test-Splat at command pipeline position 1
Supply values for the following parameters:
Greeting: 

If I use this directly without splatting, it works

Greeting: [PS] C:\>Test-Splat -Name "Frank" -Greeting "Hello"
Hello, Frank

If it's related, I'm doing this within Exchange Management Shell under PowerShell 3.0

[PS] C:\>$PSVersionTable.PSVersion

Major  Minor  Build  Revision
-----  -----  -----  --------
3      0      -1     -1
like image 809
Kirk Avatar asked Oct 15 '25 14:10

Kirk


1 Answers

You are indeed missing something, and that is when you want to splat a variable as the parameters of a function or cmdlet you use the @ symbol instead of the $ symbol. In your example, the line where you splat the variable would look like this:

Test-Splat @Params
like image 156
TheMadTechnician Avatar answered Oct 18 '25 02:10

TheMadTechnician