There are many ways to do this, depending on what you need. In this article I’m going to highlight 2 of them.
Function-style conversion:
1 2 3 4 5 6
function ArrayToHash($a) { $hash = @{} $a | foreach { $hash[$_.ProcessName] = $_ } return $hash }
Usage:
1
ArrayToHash (Get-Process)
Filter-style conversion:
1 2 3 4 5 6
filter ArrayToHash { begin { $hash = @{} } process { $hash[$_.ProcessName] = $_ } end { return $hash } }
Usage:
1
Get-Process | ArrayToHash