Next, we need to look at a concept that trips up a lot of
PowerShell beginners, and that is this idea of variable
expansion. Here is a typical string usage.
I'm creating a variable $name and giving it a value of Jeff.
And I want to use this variable to construct a string or a
message to display to the user.
For example,
I might want to show, Hello, my name is, and then fill in the
blank with $name and put in whatever $name is.
In this case, it says Jeff.
Now I'm showing you this in the console. Although this is
something you probably typically will do more when you
get to PowerShell scripting, the concepts still apply.
So what should happen and what you're expecting to happen
is that you get a result like this.
Perfect.
That's what I want.
This works great for simple values where $name is
just a simple string like Jeff.
Now this is where it gets a little tricky.
I've got the same variable, $name = "Jeff".
Now I have my string, 'Hello,
my name is $name.', but notice the quotes. I'm using single quotes here as
opposed to double quotes. When you use a single quote, PowerShell treats that
variable as an explicit or literal string that you want to display to the
screen. So variables are not expanded within single quotes. They're only
expanded or replaced with double quotes.
This is something that trips up a lot of people. And this takes it even further.
If you thought the single/double quote was kind of confusing,
let's say I have a variable, a more complex variable like a
service object. And in this case, my service object,
I can see that there are two properties,
name and status. And I want to create a string that says to the user. hey,
I basically wanted to say the BITS service is stopped.
But if you run that command, even though I'm using double quotes,
which should tell PowerShell, hey,
expand or replace the variables, this will fail.
What you're going to need to do is to use something called a subexpression,
and that's something that I'm going to get to in a moment. In fact, let's do it right now.