I was looking over what’s coming with the next WinRM and PowerShell 3 and I was looking through the list of breaking changes and saw something that I’d never seen before.
The example was:
$server = "msp42"
$status = "online"
"$server: $status"
The resulting output was:
online
OK, I’d never encountered that before and have no clue why the colon caused an issue.
A solution suggested in the document was to put a space (which is silly because then you change the output):
"$server : $status"
Another suggestion was to use this format (new to me!):
"${server}: $status"
The final suggestion was to make an expression, which I am familiar with and use all the time:
"$($server): $status"
So, my questions to you PowerShell gurus out there are:
-
What the heck is up with that colon? Does it do something?
-
What the heck is the
${variable}syntax? Is it strictly to deal with the colon or does it have some neat features?
The colon is a valid character for variable names, e.g. in
$Env:PATH, etc.You can use the following option, too
or, for some cases a format string is more readable:
Back to the colon. There is a special case for variable names that correspond to an item on a PSDrive:
The syntax
${}exists to be able to specify variable names that otherwise use characters reserved for other parts of the syntax. You could see it as being similar (but more powerful) to C#’s@in front of identifiers. See above where a\is used in the variable name, since$Drive:Itemonly works for the current container on a drive (or the root for non-hierarchic ones likeEnv,AliasorFunction).Another example where the variable name would be normally invalid syntax: