I'm trying to understand how Powershell processes variable expressions on a command line (as a parameter to a cmdlet, for instance). I can't seem to understand exactly how it parses expressions involving multiple variables (and/or properties on variables). Here are some example based on the following predefined variables:
$a = 'abc'
$b = 'def'
$f = Get-ChildItem .\Test.txt # assuming such a file exists in the current directory
Example 1:
echo $a$b
Output: abcdef
Example 2:
echo $a\$b
Output: abc\def
Example 3:
echo $f.BaseName
Output: Test
Example 4:
echo $a\$f.BaseName
Output: abc\C:\Test.txt.BaseName
Basically, I don't understand why I can combine two variables (examples 1 and 2), and I can use variable properties (example 3), but I can't combine variables with other variable properties (example 4). I've tried various escape sequences (using the backtick) to no avail.
I realize I can accomplish this using $()
style expressions, like:
echo $($a + '\' + $f.BaseName)
I just don't understand why the other form (example 4) is invalid--it reads cleaner in my opinion.