1
00:00:01,140 --> 00:00:04,190
Next, we need to look at a concept that trips up a lot of

2
00:00:04,190 --> 00:00:07,300
PowerShell beginners, and that is this idea of variable

3
00:00:07,300 --> 00:00:10,700
expansion. Here is a typical string usage.

4
00:00:10,700 --> 00:00:14,380
I'm creating a variable $name and giving it a value of Jeff.

5
00:00:14,380 --> 00:00:17,540
And I want to use this variable to construct a string or a

6
00:00:17,540 --> 00:00:20,040
message to display to the user.

7
00:00:20,040 --> 00:00:20,790
For example,

8
00:00:20,790 --> 00:00:24,520
I might want to show, Hello, my name is, and then fill in the

9
00:00:24,520 --> 00:00:27,490
blank with $name and put in whatever $name is.

10
00:00:27,490 --> 00:00:29,180
In this case, it says Jeff.

11
00:00:29,180 --> 00:00:32,880
Now I'm showing you this in the console. Although this is

12
00:00:32,880 --> 00:00:35,300
something you probably typically will do more when you

13
00:00:35,300 --> 00:00:39,340
get to PowerShell scripting, the concepts still apply.

14
00:00:39,340 --> 00:00:42,140
So what should happen and what you're expecting to happen

15
00:00:42,140 --> 00:00:45,340
is that you get a result like this.

16
00:00:45,340 --> 00:00:45,750
Perfect.

17
00:00:45,750 --> 00:00:47,140
That's what I want.

18
00:00:47,140 --> 00:00:50,870
This works great for simple values where $name is

19
00:00:50,870 --> 00:00:53,740
just a simple string like Jeff.

20
00:00:53,740 --> 00:00:56,640
Now this is where it gets a little tricky.

21
00:00:56,640 --> 00:00:58,930
I've got the same variable, $name = "Jeff".

22
00:00:58,930 --> 00:01:01,790
Now I have my string, 'Hello,

23
00:01:01,790 --> 00:01:07,270
my name is $name.', but notice the quotes. I'm using single quotes here as

24
00:01:07,270 --> 00:01:13,270
opposed to double quotes. When you use a single quote, PowerShell treats that

25
00:01:13,270 --> 00:01:19,990
variable as an explicit or literal string that you want to display to the

26
00:01:19,990 --> 00:01:25,460
screen. So variables are not expanded within single quotes. They're only

27
00:01:25,460 --> 00:01:29,760
expanded or replaced with double quotes.

28
00:01:29,760 --> 00:01:34,710
This is something that trips up a lot of people. And this takes it even further.

29
00:01:34,710 --> 00:01:38,340
If you thought the single/double quote was kind of confusing,

30
00:01:38,340 --> 00:01:41,540
let's say I have a variable, a more complex variable like a

31
00:01:41,540 --> 00:01:44,560
service object. And in this case, my service object,

32
00:01:44,560 --> 00:01:46,080
I can see that there are two properties,

33
00:01:46,080 --> 00:01:51,650
name and status. And I want to create a string that says to the user. hey,

34
00:01:51,650 --> 00:01:55,940
I basically wanted to say the BITS service is stopped.

35
00:01:55,940 --> 00:01:59,370
But if you run that command, even though I'm using double quotes,

36
00:01:59,370 --> 00:02:01,210
which should tell PowerShell, hey,

37
00:02:01,210 --> 00:02:05,840
expand or replace the variables, this will fail.

38
00:02:05,840 --> 00:02:10,140
What you're going to need to do is to use something called a subexpression,

39
00:02:10,140 --> 00:02:16,000
and that's something that I'm going to get to in a moment. In fact, let's do it right now.

