I've got a hashtable:
$hash = @{ First = 'Al'; Last = 'Bundy' }
I know that I can do this:
Write-Host "Computer name is ${env:COMPUTERNAME}"
So I was hoping to do this:
Write-Host "Hello, ${hash.First} ${hash.Last}."
...but I get this:
Hello, .
How do I reference hash table members in string interpolation?
In the first method, the one that I prefer, you can use the GetEnumerator method of the hash table object. In the second method, instead of iterating over the hash table itself, we loop over the Keys of the hash table. Both of these methods produce the same output as our original version.
What is @() in PowerShell Script? In PowerShell, the array subexpression operator “@()” is used to create an array. To do that, the array sub-expression operator takes the statements within the parentheses and produces the array of objects depending upon the statements specified in it.
PowerShell declares a variable by using the $ sign and the variable name. For example, $Services. When we declare any type (Integer, double, string, DateTime) of the variable and when we use them inside the string variable, it automatically converts that variable to the string.
Write-Host "Hello, $($hash.First) $($hash.Last)."
"Hello, {0} {1}." -f $hash["First"] , $hash["Last"]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With