Passing hash table (associative array) into scriptblock RRS feed

  • Question

  • Hi,

    Does anyone know how to pass a hash table into a scriptblock in PowerShell?


    Invoke-Command @commandParameters -ArgumentList arg1, arg2, arg3 (assuming arg3 is the hash table)


    • Moved by EugeneLeitanMVP Wednesday, September 7, 2016 9:55 AM it isn't about the SCDPM
    Tuesday, April 5, 2016 7:46 AM


  • I found the answer myself and it works for me. This is how I build the associative array and then pass it to the script block. I was using the dot notation to reference the hash table, but it looks like I need to use [ ] in the script block.

    $compADGroups = @{}

    foreach ( $adGroup in $adGroups ) {

    if ( $compADGroups.$computerNameGroup -eq $null ) {  
        $compADGroups[$computerName] = @{}  
        $compADGroups[$computerName]["Group"] = @{} 
        $compADGroups[$computerName]["Group"] = $hashString                            


    $session = New-PSSession -ComputerName 'Computer1'

    Invoke-Command -Session $session -ArgumentList $compADGroups -ScriptBlock { param($compADGroups) $compADGroups[$env:computername]["Group"]}

    Get-PSSession | Remove-PSSession

    Tuesday, April 5, 2016 6:45 PM