Powershell: splatting after passing hashtable by reference

Posted by user1815871 on Stack Overflow See other posts from Stack Overflow or by user1815871
Published on 2012-11-11T10:56:45Z Indexed on 2012/11/11 10:59 UTC
Read the original article Hit count: 318

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.)

© Stack Overflow or respective owner

Related posts about powershell

Related posts about hashtable