1
00:00:01,140 --> 00:00:02,320
Before we wrap up,

2
00:00:02,320 --> 00:00:05,880
there is one more area I want to touch on because it can

3
00:00:05,880 --> 00:00:08,840
affect how you run commands in PowerShell.

4
00:00:08,840 --> 00:00:11,120
This will especially become important when you start writing

5
00:00:11,120 --> 00:00:14,840
your own PowerShell scripts and functions.

6
00:00:14,840 --> 00:00:19,530
Now, PowerShell by itself is nothing more than an engine, like a car engine.

7
00:00:19,530 --> 00:00:25,040
It needs to be inserted into a vehicle in order to really use it.

8
00:00:25,040 --> 00:00:29,440
In our case, PowerShell has to be hosted in an application.

9
00:00:29,440 --> 00:00:32,360
Now you don't have to worry about this other than understand that

10
00:00:32,360 --> 00:00:37,440
there is a difference between the host and PowerShell.

11
00:00:37,440 --> 00:00:42,740
When you run a PowerShell command, it writes objects to the PowerShell pipeline.

12
00:00:42,740 --> 00:00:45,240
This is what allows you to do a lot of work from a

13
00:00:45,240 --> 00:00:47,840
Command Prompt with minimal effort.

14
00:00:47,840 --> 00:00:48,670
For the most part,

15
00:00:48,670 --> 00:00:54,040
this is how you should be working with PowerShell; objects in the pipeline.

16
00:00:54,040 --> 00:00:59,440
However, you can also interact directly with the hosting application.

17
00:00:59,440 --> 00:01:05,640
There are commands in PowerShell that let you write to and read from the host.

18
00:01:05,640 --> 00:01:08,840
It is the writing part that trips up a lot of beginners.

19
00:01:08,840 --> 00:01:13,940
You can only work with objects that are written to the PowerShell pipeline.

20
00:01:13,940 --> 00:01:16,880
If you write to the host, all you can do is read and

21
00:01:16,880 --> 00:01:20,440
write whatever is on the screen.

22
00:01:20,440 --> 00:01:25,140
And this will be much easier to see in a demonstration.

23
00:01:25,140 --> 00:01:28,940
I need to make sure you understand about the PowerShell host.

24
00:01:28,940 --> 00:01:32,560
Understand why commands in the pipeline are really important.

25
00:01:32,560 --> 00:01:36,230
Make sure you understand the ins and outs of using write‑host.

26
00:01:36,230 --> 00:01:37,620
And since we're at it,

27
00:01:37,620 --> 00:01:40,450
I might as well show you read‑host, although this is something

28
00:01:40,450 --> 00:01:43,760
that you probably really won't get into until you start doing

29
00:01:43,760 --> 00:01:46,440
your own PowerShell scripting.

30
00:01:46,440 --> 00:01:46,800
All right,

31
00:01:46,800 --> 00:01:51,420
so let me show you what I'm talking about between the host and PowerShell.

32
00:01:51,420 --> 00:01:52,510
And when I say PowerShell,

33
00:01:52,510 --> 00:01:55,590
what I'm really talking about is the PowerShell pipeline.

34
00:01:55,590 --> 00:01:58,940
This is something that trips up a lot of PowerShell beginners.

35
00:01:58,940 --> 00:02:01,380
So I'm going to look at a built‑in variable called $host.

36
00:02:01,380 --> 00:02:04,490
So you can see that I'm running in the ConsoleHost, and

37
00:02:04,490 --> 00:02:07,440
that also shows you the version.

38
00:02:07,440 --> 00:02:10,240
And I'm going to do a couple things here.

39
00:02:10,240 --> 00:02:11,960
They'll all look the same.

40
00:02:11,960 --> 00:02:17,740
So I'm just going to type the string "apple," and PowerShell shows me apple.

41
00:02:17,740 --> 00:02:21,640
I'm going to use a cmdlet called Write‑Output,

42
00:02:21,640 --> 00:02:25,640
and I'll do the same thing with Write‑Host.

43
00:02:25,640 --> 00:02:31,040
Now, as far as you can tell, it looks like they all did the same thing.

44
00:02:31,040 --> 00:02:33,880
And if all you're doing is just reading what's on the screen, yeah,

45
00:02:33,880 --> 00:02:38,740
maybe it really doesn't matter, but it will matter.

46
00:02:38,740 --> 00:02:43,190
So I'm going to take that string "apple" and pipe it to Get‑Member,

47
00:02:43,190 --> 00:02:49,040
which is a command that I'm going to cover in the next module.

48
00:02:49,040 --> 00:02:52,490
So this shows me that "apple" is a string object and Get‑Member shows

49
00:02:52,490 --> 00:02:56,940
me all of the methods and properties of that object.

50
00:02:56,940 --> 00:03:00,740
This works because something was written to the pipeline

51
00:03:00,740 --> 00:03:04,140
for Get‑Member to know what to do with.

52
00:03:04,140 --> 00:03:09,460
Let's take write‑output, repeat the process. Again, I get

53
00:03:09,460 --> 00:03:12,170
an object written to the pipeline that Get‑Member can

54
00:03:12,170 --> 00:03:15,940
consume and do something with.

55
00:03:15,940 --> 00:03:20,740
Now let's try with Write‑Host.

56
00:03:20,740 --> 00:03:24,420
I see the string there apple, but I also now get an error message.

57
00:03:24,420 --> 00:03:27,330
You must specify an object to the Get‑Member cmdlet,

58
00:03:27,330 --> 00:03:29,180
and this is the big distinction.

59
00:03:29,180 --> 00:03:33,720
Write‑Host does exactly what the name is implying.

60
00:03:33,720 --> 00:03:36,740
It's writing to the hosting application.

61
00:03:36,740 --> 00:03:39,940
It's not writing output to the pipeline.

62
00:03:39,940 --> 00:03:44,240
It's not writing to PowerShell.

63
00:03:44,240 --> 00:03:46,480
Now, this can be sometimes useful, by the way,

64
00:03:46,480 --> 00:03:50,110
so don't don't get the idea that Write‑Host is totally bad.

65
00:03:50,110 --> 00:03:51,860
If we look at help for Write‑Hosts,

66
00:03:51,860 --> 00:03:56,630
you'll see that there are some options there where you can specify formatting

67
00:03:56,630 --> 00:04:00,390
for colors of the whatever it is that you want to write.

68
00:04:00,390 --> 00:04:04,240
So if I wanted to, I could write a message to the console saying,

69
00:04:04,240 --> 00:04:08,170
just kind of silly here, "A is for Apple," but display it in a red

70
00:04:08,170 --> 00:04:13,980
color. When I see that, I now know that that output is not part of

71
00:04:13,980 --> 00:04:18,340
my script or not part of the command, it's written directly to the host.

72
00:04:18,340 --> 00:04:22,240
Now here's where it kind of gets interesting now that I think about it.

73
00:04:22,240 --> 00:04:27,670
We now have all this capability of doing ANSI formatting, so you could

74
00:04:27,670 --> 00:04:31,750
actually write an ANSI‑colored string to the pipeline.

75
00:04:31,750 --> 00:04:35,840
It would be displayed in color, but would also be written to the pipeline.

76
00:04:35,840 --> 00:04:42,890
So my little idea of only using color with Write‑Host may or may not be useful,

77
00:04:42,890 --> 00:04:45,640
but it is an option there.

78
00:04:45,640 --> 00:04:49,790
The difference that you have to keep in mind is you can only deal

79
00:04:49,790 --> 00:04:52,980
with objects that you have written to the pipeline using

80
00:04:52,980 --> 00:04:56,720
Write‑Output, what you really don't need, just let PowerShell do its

81
00:04:56,720 --> 00:04:59,080
thing, and then consume those objects.

82
00:04:59,080 --> 00:05:03,070
Using Write‑Host is telling PowerShell, take whatever this is

83
00:05:03,070 --> 00:05:05,320
and just display it to me so I can look at it.

84
00:05:05,320 --> 00:05:10,840
I really can't do anything else with it.

85
00:05:10,840 --> 00:05:12,440
All right, so here's something else.

86
00:05:12,440 --> 00:05:16,480
I'm going to create a variable called "Jeff." Most of the

87
00:05:16,480 --> 00:05:17,910
time, that's really all you need to do.

88
00:05:17,910 --> 00:05:19,150
Just define the variable,

89
00:05:19,150 --> 00:05:22,570
assign it a value, and you're ready to go, and you can use it.

90
00:05:22,570 --> 00:05:26,640
I can just do $Name and it shows me the name of the variable.

91
00:05:26,640 --> 00:05:28,020
You can, however,

92
00:05:28,020 --> 00:05:33,000
use the hosting capabilities to prompt using Read‑Host for a

93
00:05:33,000 --> 00:05:37,800
value. So I could say Read‑Host and give it a prompt, hey, "Enter

94
00:05:37,800 --> 00:05:43,640
a service name." And let's put in bits.

95
00:05:43,640 --> 00:05:46,970
So now $name is the name of a service,

96
00:05:46,970 --> 00:05:51,500
and if I do Get‑Service, or using the alias gsv, and my

97
00:05:51,500 --> 00:05:55,440
variable, I can see that is working.

98
00:05:55,440 --> 00:06:02,140
This also works, this Read‑Host for credentials, or should I say secure strings.

99
00:06:02,140 --> 00:06:10,940
So I can say, hey, enter a credential for the password for the username.

100
00:06:10,940 --> 00:06:14,340
Now, put something in. Now that Get‑Credential, by the way,

101
00:06:14,340 --> 00:06:17,400
all it's doing is allowing you to input a credential.

102
00:06:17,400 --> 00:06:20,690
I have no idea if that password I put in was correct or not.

103
00:06:20,690 --> 00:06:23,940
I won't know until I use that.

104
00:06:23,940 --> 00:06:27,590
But you can see, though, that it masked the password and I now

105
00:06:27,590 --> 00:06:30,960
have a credential object that I could use with other commands

106
00:06:30,960 --> 00:06:33,490
that might take a credential object.

107
00:06:33,490 --> 00:06:36,740
I'm not going to demo anything like that right now.

108
00:06:36,740 --> 00:06:40,100
You probably really aren't going to use Read‑Host until you start scripting,

109
00:06:40,100 --> 00:06:45,260
but I wanted you to see that it is available if you were doing something

110
00:06:45,260 --> 00:06:52,040
interactively and you kind of wanted to be prompted or take advantage of that.

111
00:06:52,040 --> 00:06:53,900
I've gone through a lot of demo in this module.

112
00:06:53,900 --> 00:06:55,390
If you felt a little lost,

113
00:06:55,390 --> 00:06:59,290
that's okay, and actually to be expected. Download the demo files and

114
00:06:59,290 --> 00:07:02,340
try some of the commands in PowerShell yourself.

115
00:07:02,340 --> 00:07:02,520
Now,

116
00:07:02,520 --> 00:07:04,460
I know some people get worked up and feel a little bit

117
00:07:04,460 --> 00:07:06,940
nervous about working at a Command Prompt.

118
00:07:06,940 --> 00:07:07,800
Relax.

119
00:07:07,800 --> 00:07:09,570
You're not going to break anything.

120
00:07:09,570 --> 00:07:11,350
I'm trusting you're trying everything out here in a

121
00:07:11,350 --> 00:07:15,150
non‑critical desktop. Using PowerShell to get your job

122
00:07:15,150 --> 00:07:17,240
done is a simple three‑step process.

123
00:07:17,240 --> 00:07:20,090
First, find the command you think you need to use.

124
00:07:20,090 --> 00:07:24,740
Read the help and examples. And then lastly, run the command from the prompt.

125
00:07:24,740 --> 00:07:26,680
Don't worry about scripting right now.

126
00:07:26,680 --> 00:07:29,900
Focus on running commands from the PowerShell prompt.

127
00:07:29,900 --> 00:07:30,860
So that's it for now.

128
00:07:30,860 --> 00:07:32,360
I suggest you take a quick break,

129
00:07:32,360 --> 00:07:38,000
stretch your legs, and then come on back to learn more about putting PowerShell to work.

