# How to merge two hash tables into one hash table

**URL:** <https://forums.powershell.org/t/how-to-merge-two-hash-tables-into-one-hash-table/21240>\
**Category:** PowerShell Help\
**Created:** [December 28, 2022, 8:35am UTC](https://forums.powershell.org/t/how-to-merge-two-hash-tables-into-one-hash-table/21240 "2022-12-28T08:35:14Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ben](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@Ben](https://forums.powershell.org/u/Ben)\
**Post date:** [December 28, 2022, 8:35am UTC](https://forums.powershell.org/t/how-to-merge-two-hash-tables-into-one-hash-table/21240/1 "2022-12-28T08:35:14Z")

</div>

We capture the average memory usage of Azure VM through kql, but the KQL statements of our Windows and Linux are different, so they cannot be generated into a hash table, and finally cannot be imported into a sheet of excel. Is there a way to Combining two hash tables into one

```auto
Connect-AzAccount
Set-AzContext -Subscription ""
$workspaceName = ""
$workspaceRG = ""
$WorkspaceID = (Get-AzOperationalInsightsWorkspace -Name $workspaceName -ResourceGroupName $workspaceRG).CustomerID
# vm_windows_memory_begin
$windows_memoryquery = "
Perf
| where TimeGenerated > ago(30min) and ObjectName == 'Memory'
| where CounterName contains 'Available Bytes'
| summarize Free=avg(CounterValue) by Computer,bin(TimeGenerated, 30m)
|join kind= inner
(
    Perf
    | where ObjectName == 'Memory'
    | where CounterName == 'Committed Bytes'
    | summarize Used=avg(CounterValue) by Computer,bin(TimeGenerated, 30m)
    | summarize arg_max(TimeGenerated, *) by Computer
)
on Computer
| project TimeGenerated, UsedPCT=round(Used * 100 /(Free + Used),2), Computer
| render timechart"
$vm_memorykqlQuery = Invoke-AzOperationalInsightsQuery -WorkspaceId $WorkspaceID -Query $windows_memoryquery
$vm_memorykql = foreach ($a in $vm_memorykqlQuery.Results) {
    $props = @{
        'Computer' = $a.Computer;
        'UsedPCT' = $a.UsedPCT;
    }
    New-Object -TypeName PSObject -Property $props
}
$WindowsMemory = foreach ($a in $vm_memorykql.Computer | Select-Object -Unique) {
    $props = @{
        'Hostname' = $a;
        'avg_CounterValue' = ("{0:N2}" -f (($vm_memorykql | Where-Object { $_.Computer -eq $a }).UsedPCT | Measure-Object -Average).Average);
    }
    New-Object -TypeName PSObject -Property $props
}
$WindowsMemory | Export-Excel -Path C:\temp\test.xlsx -WorksheetName WindowsMemory
# vm_windows_memory_end
# vm_linux_memory_begin
$linux_memoryquery = "
Perf
| where TimeGenerated > ago(7d)
| where CounterName == '% Used Memory'
| where ObjectName == 'Memory'
| summarize avg(CounterValue) by bin(TimeGenerated, 30min), Computer
| render timechart"
$linux_memorykqlQuery = Invoke-AzOperationalInsightsQuery -WorkspaceId $WorkspaceID -Query $linux_memoryquery
$linux_memorykql = foreach ($a in $linux_memorykqlQuery.Results) {
    $props = @{
        'Computer' = $a.Computer;
        'avg_CounterValue' = $a.avg_CounterValue;
    }
    New-Object -TypeName PSObject -Property $props
}
$linux_memory = foreach ($a in ($linux_memorykql.Computer | Select-Object -Unique)) {
    $props = @{
        'Hostname' = $a;
        'avg_CounterValue' = ("{0:N2}" -f (($linux_memorykql | Where-Object { $_.Computer -eq $a }).avg_CounterValue | Measure-Object -Average).Average);
    }
    New-Object -TypeName PSObject -Property $props
}
$linux_memory | Export-Excel -Path C:\temp\test.xlsx -WorksheetName linux_memory
# vm_linux_memory_end

```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/powershell/original/1X/385e4f9fe133561ad30a814262fe0e0ae6bc3ef0.png) [@system](https://forums.powershell.org/u/system)\
**Post date:** [February 10, 2024, 5:34pm UTC](https://forums.powershell.org/t/how-to-merge-two-hash-tables-into-one-hash-table/21240/2 "2024-02-10T17:34:05Z")

</div>

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.
