I don’t understand why there’s a difference on string concatenations in this script.
I really don’t understand where the whitespace is coming from?
Can you tell me what’s wrong?
$table = @{
"aaa"=1
}
$x = "qqq"
$y = "rrr"
$table.GetEnumerator() | ForEach-Object {
Write-Host $_.Key$x #THIS PRINTS "aaa qqq"
}
Write-Host $x$y #THIS PRINTS : "qqqrrr"
When processing arguments to a command, the PowerShell parser splits adjacent expressions into discrete arguments if the first fragment is a recognizable expression as is the case with $(). Because PowerShell is object-based, we try to preserve object integrity until we absolutely have to render to a string. If you want to force string expansion put double-quotes around the entire argument sequence as one of the other posters suggested. Note that an argument with a leading character like a$x will be treated as if it was a double-quoted string “a$x”. This is because the leading characters are not recognized as a valid expression so we default to treating it as an expandable string.
-bruce
Bruce Payette,
Windows PowerShell Team,
Microsoft Corp.