Powershell: splatting after passing hashtable by reference
- by user1815871
Powershell newbie ... I recently learned about splatting very useful. I ran into a snag when I passed a hash table by reference to a function for splatting purposes. (For brevity's sake a silly example.)
Function AllMyChildren {
param (
[ref]$ReferenceToHash
}
get-childitem @ReferenceToHash.Value
# etc.etc.
}
$MyHash = @{
'path' = '*'
'include' = '*.ps1'
'name' = $null
}
AllMyChildren ([ref]$MyHash)
Result: an error ("Splatted variables cannot be used as part of a property or array expression. Assign the result of the expression to a temporary variable then splat the temporary variable instead."). Tried this afterward:
$newVariable = $ReferenceToHash.Value
get-childitem @NewVariable
That did work and seemed right per the error message. But: is it the preferred syntax in a case like this? (An oh, look, it actually worked solution isn't always a best practice. My approach here strikes me as "Perl-minded" and perhaps in Powershell passing by value is better, though I don't yet know the syntax for it w.r.t. a hash table.)