Powershell: Splatting a property

Created on 25 Oct 2017  路  3Comments  路  Source: PowerShell/PowerShell

Summary

It would be helpful if we could splat using the property of a variable (i.e. where that property is itself a hashtable) to a command.

Code Example:

function Test-Demo {
    [CmdletBinding()]
    Param (
        [Parameter()]
        [string]$One
        ,
        [Parameter()]
        [string]$Two
    )
    "1 = $One"
    "2 = $Two"
}
$test = @{
    testInt = @{ 
        One = '1'
        Two = '2'
    }
    testString = @{
        One = 'One'
        Two = 'Two'
    }
}

#to splat a property, we first need to assign the property to a "top level" variable:
$t = $test.testString
Test-Demo @t 

#however it would be nice if we could instead do this:
Test-Demo @test.testString 

#or if that's not possible for some reason, maybe something like this:
Test-Demo ([splat]$test.testString)

This could be particularly useful when dealing with config files. e.g. Our config holds database information (instance, catalog, credentials), and also sections with other properties specific to our functions. e.g.

$config = Get-MyScriptConfig -Path '.\Config.xml'
Invoke-Something @config.DatabaseConnectionInfo @config.Something
Issue-Question Resolution-Answered

Most helpful comment

Sounds like we need to revive https://github.com/PowerShell/PowerShell-RFC/issues/6 - the RFC draft suggests generalized expression splatting, with syntax like:

Test-Demo @$test.testString

All 3 comments

Sounds like we need to revive https://github.com/PowerShell/PowerShell-RFC/issues/6 - the RFC draft suggests generalized expression splatting, with syntax like:

Test-Demo @$test.testString

@JohnLBevan Can we close the Issue looking https://github.com/PowerShell/PowerShell-RFC/issues/6 ?

@iSazonov ; yes, happy for this to be closed given it's already covered there. Thank-you.

Was this page helpful?
0 / 5 - 0 ratings