The solution has been already provided by @PetSerAl and @LotPings and is one of the following
$alpha.GetEnumerator() | select Key, @{ n='Value'; e={$beta[$_.Value]} }
$alpha.GetEnumerator() | %{[PSCustomObject]@{aKey=$_.Key;aValue=$_.Value;bValue=$beta[$_.Value]}}
Let me explain what exactly happens there.
First of all, as you use hashtables you cannot manipulate them directly using cmdlets like Select-Object. In order to do this you need to use GetEnumerator() method on it. Now you can pipe it to Select-Object.
To use the values from another hashtable you have to use calculated property instead of standard one. The syntax of it is:
@{ n='name'; e={ expression to be executed }
Let's dig into this expression $beta[$_.Value] a bit more. $_ represents the object sent to pipeline so $_.Value is its value (as you know hashtables have key names and values). To better understand check this expression and its result
PS C:\> $alpha.GetEnumerator() | select -Last 1
Name Value
---- -----
A5 dog
For this entry $_.Value is dog so $beta[$_.Value] is evaluated to $beta["dog"] and its value is:
PS C:\> $beta["dog"]
P5
Additional resources:
- Microcode: PowerShell Scripting Tricks: The Joy of using Hashtables
with Windows
PowerShell
- Add a calculated property with Select-Object in PowerShell
- Example of the same but with multiple values to be replaced (my answer)
$alpha.keyscompare-objectand "manipulate with$alpha.keys" code look like? What output did you get (if any) and how does that differ from what you were expecting?planein P2 and A3.