This page looks best with JavaScript enabled

PowerShell - Convert Array To HashTable

 ·   ·  ☕ 1 min read

There are many ways to do this, depending on what you need. In this article I’m going to highlight 2 of them.

  1. Function-style conversion:

    1
    2
    3
    4
    5
    6
    
    function ArrayToHash($a)
    {
        $hash = @{}
        $a | foreach { $hash[$_.ProcessName] = $_ }
        return $hash
    }
    

    Usage:

    1
    
    ArrayToHash (Get-Process)
    
  2. Filter-style conversion:

    1
    2
    3
    4
    5
    6
    
    filter ArrayToHash
    {
        begin { $hash = @{} }
        process { $hash[$_.ProcessName] = $_ }
        end { return $hash }
    }
    

    Usage:

    1
    
    Get-Process | ArrayToHash
    

Victor Zakharov
WRITTEN BY
Victor Zakharov
Web Developer (Angular/.NET)