Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Formatting a Powershell string containing hashtable values

The answer to this is likely to be trivial, but I have spent half an hour and still can't work it out.

Assume I have a the following hashtable:

$hash = @{face='Off';}

What I have tried to do, is output the value of "face" along side some other string elements.

This works:

Write-Host Face $hash['face']
=> Face Off

However, this doesn't:

Write-Host Face/$hash['face']
=> Face/System.Collections.Hashtable[face]

Somehow the lack of a space has affected the operator precedence - it is now evaluating $hash as a string, the concatenating [face] afterwards.

Trying to solve this precedence problem, I tried:

Write-Host Face/($hash['face'])
=> Face/ Off

I now have an extra space I don't want. This works, but I don't want the extra line just to reassign:

$hashvalue = $hash['face']
write-host Face/$hashvalue
=> Face/Off

Any idea how to get this working as a one-liner?

like image 259
llevera Avatar asked Mar 02 '12 21:03

llevera


People also ask

How do you use a variable inside a string in PowerShell?

PowerShell has another option that is easier. You can specify your variables directly in the strings. $message = "Hello, $first $last." The type of quotes you use around the string makes a difference.

How do I iterate through a Hashtable in PowerShell?

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.

How do I combine strings and variables in PowerShell?

In PowerShell, string concatenation is primarily achieved by using the “+” operator. There are also other ways like enclosing the strings inside double quotes, using a join operator, or using the -f operator.

How does PowerShell store key value pairs?

Create a Hash table and immediately populate it with some values: $array_name = @{key1 = item1; key2 = item2;...} Notice that placing quotes around the key is optional (unless the key contains spaces). Each key/value pair must either be placed on a separate line or separated with a semicolon delimiter.


3 Answers

Sure, use a subexpression:

Write-Host Face/$($hash['face'])

Generally, I'd just use a string, if I need precise control over whitespace with Write-Host:

Write-Host "Face/$($hash['face'])"

Yes, in this case you need a subexpression again, but more because you simply can't include an expression like $foo['bar'] in a string otherwise ;-)

By the way, $hash.face works just as well with much less visual clutter.

like image 93
Joey Avatar answered Sep 18 '22 01:09

Joey


In such cases, and more so if there are more variables involved, I prefer using the string formatting. While in this case you can live with the $(..), be aware that string formatting will remove lot of doubt, cruft and improves readability:

write-host ("Face/{0}" -f $hash['face'])
like image 35
manojlds Avatar answered Sep 19 '22 01:09

manojlds


In addition to using sub expressions as Joey showed you can:

Use string formatting:

Write-Host ('Face/{0}' -f $hash.face)

This will stick the value of face key in the place of {0}

Use string concatenation:

Write-Host ('Face/' + $hash.face)

Both of those require an expression to be evaluated which outputs a string which is used as Write-Host's Object parameter.

like image 22
Andy Arismendi Avatar answered Sep 21 '22 01:09

Andy Arismendi