1
00:00:00,240 --> 00:00:03,510
So let's go back into the console and onto my Windows machine,

2
00:00:03,510 --> 00:00:08,140
and we'll execute a script directly in the PowerShell console first.

3
00:00:08,140 --> 00:00:13,240
We'll then execute a script within the PowerShell ISE, so the integrated studio.

4
00:00:13,240 --> 00:00:17,140
We'll then execute a script within the Windows terminal.

5
00:00:17,140 --> 00:00:17,950
And then, lastly,

6
00:00:17,950 --> 00:00:22,140
we'll look at how we execute a script within Visual Studio Code.

7
00:00:22,140 --> 00:00:25,650
Now when it comes to creating PowerShell scripts or even executing them,

8
00:00:25,650 --> 00:00:28,830
we have a few different options that are available to us.

9
00:00:28,830 --> 00:00:32,340
So I'm going to close my terminal down, and we'll launch that again.

10
00:00:32,340 --> 00:00:35,920
What I'm going to do first is click the Start menu and just type the word

11
00:00:35,920 --> 00:00:39,120
PowerShell and literally just go to the Windows PowerShell option,

12
00:00:39,120 --> 00:00:41,340
right‑click and choose administrator.

13
00:00:41,340 --> 00:00:47,880
And this will take me to the actual PowerShell one that's out by default.

14
00:00:47,880 --> 00:00:50,630
Now, of course, you can see it's pretty glaring colors here,

15
00:00:50,630 --> 00:00:55,650
so let's just go to Colors, and I'll change the color to be a bit darker blue.

16
00:00:55,650 --> 00:00:56,840
There we go, a bit nicer.

17
00:00:56,840 --> 00:01:01,090
Now this is the standard Windows PowerShell interface

18
00:01:01,090 --> 00:01:03,940
that's available on all Windows machines.

19
00:01:03,940 --> 00:01:05,270
Really straightforward.

20
00:01:05,270 --> 00:01:08,700
You launch it, and you can do all the standard.

21
00:01:08,700 --> 00:01:11,860
So if I do Get‑ChildItem, for example, and just execute,

22
00:01:11,860 --> 00:01:13,770
it'll give me a list of files.

23
00:01:13,770 --> 00:01:17,760
It is color‑coded, so you can see it's fairly straightforward.

24
00:01:17,760 --> 00:01:22,860
If I want to change directory, let's say I want to go to \Users\Trainer\Data\,

25
00:01:22,860 --> 00:01:26,340
then I can change directory.

26
00:01:26,340 --> 00:01:30,250
And if I wanted to execute a PowerShell script,

27
00:01:30,250 --> 00:01:32,720
I'm just going to say .\Script.ps1.

28
00:01:32,720 --> 00:01:35,930
And sure enough, it just executes the script automatically.

29
00:01:35,930 --> 00:01:37,680
So really, really straightforward.

30
00:01:37,680 --> 00:01:41,110
This is the most common way to execute scripts.

31
00:01:41,110 --> 00:01:43,380
You just launch that Windows PowerShell console.

32
00:01:43,380 --> 00:01:46,280
An actual fact, when you use other applications,

33
00:01:46,280 --> 00:01:49,750
for example, server products like SharePoint or even Exchange,

34
00:01:49,750 --> 00:01:54,340
when you launch their version of the PowerShell console,

35
00:01:54,340 --> 00:01:58,940
it's actually just built on top of the regular Windows one that's there.

36
00:01:58,940 --> 00:02:00,930
Okay, so let's just exit that one.

37
00:02:00,930 --> 00:02:04,380
The second option available to us, I'm going to type PowerShell again,

38
00:02:04,380 --> 00:02:06,360
is the one that's called PowerShell ISE.

39
00:02:06,360 --> 00:02:08,400
And we've touched on this in the past,

40
00:02:08,400 --> 00:02:12,440
so I'm going to launch this again and wait for that to load.

41
00:02:12,440 --> 00:02:15,190
Now, of course, I've got some extra pieces here,

42
00:02:15,190 --> 00:02:17,270
so I'm just going to drag this to the right.

43
00:02:17,270 --> 00:02:21,920
And what you'll see from here is we get a canvas at the top for typing.

44
00:02:21,920 --> 00:02:24,550
So let's say I was going to say Get‑ChildItem again.

45
00:02:24,550 --> 00:02:29,160
I can select it, and execute, and it shows the results in the bottom.

46
00:02:29,160 --> 00:02:30,970
At any point, of course,

47
00:02:30,970 --> 00:02:34,700
if I'm not sure what modules I have loaded or I want to

48
00:02:34,700 --> 00:02:37,410
know how to do something or I'm not sure,

49
00:02:37,410 --> 00:02:41,300
let's say I wasn't sure what Get‑ChildItem actually does,

50
00:02:41,300 --> 00:02:44,400
I can actually search for it here, click Show Details,

51
00:02:44,400 --> 00:02:49,840
and then it will list me all of the properties that are part of that function.

52
00:02:49,840 --> 00:02:51,950
It also gives me the ability to kind of,

53
00:02:51,950 --> 00:02:54,300
if I just type some values here, for example,

54
00:02:54,300 --> 00:02:56,900
just arbitrary, I can say Insert,

55
00:02:56,900 --> 00:03:00,540
and it will populate that into the bottom section.

56
00:03:00,540 --> 00:03:04,110
I can also copy that and paste it somewhere else, too.

57
00:03:04,110 --> 00:03:05,790
So it's a nice interface.

58
00:03:05,790 --> 00:03:11,700
I really like using Windows PowerShell ISE just because it's a clean interface,

59
00:03:11,700 --> 00:03:13,790
you can quickly see the code, you can execute,

60
00:03:13,790 --> 00:03:16,640
you can do all kinds of things with it as you need to.

61
00:03:16,640 --> 00:03:16,910
Now,

62
00:03:16,910 --> 00:03:20,470
the only downside here is that we're not sure exactly how long

63
00:03:20,470 --> 00:03:23,510
this is going to last because it's not really been invested in

64
00:03:23,510 --> 00:03:26,040
as an application on the machines.

65
00:03:26,040 --> 00:03:28,840
So, what do we look at replacing that with?

66
00:03:28,840 --> 00:03:30,190
Well, first off,

67
00:03:30,190 --> 00:03:33,470
you've noticed that we've been using what's called the Windows Terminal,

68
00:03:33,470 --> 00:03:36,630
so I'm going to right‑click and do Run as administrator.

69
00:03:36,630 --> 00:03:40,410
This is the old sing and new dancing kind of interface.

70
00:03:40,410 --> 00:03:44,830
What's nice about this one is that you can kind of have the ability to

71
00:03:44,830 --> 00:03:47,540
flick different versions of tabs between each other.

72
00:03:47,540 --> 00:03:50,500
So for the predominantly, we've been using PowerShell 7,

73
00:03:50,500 --> 00:03:54,520
but of course, I did flick to Windows PowerShell to look at signing,

74
00:03:54,520 --> 00:03:58,320
but I could also go to a command prompt or Azure Cloud Shell or

75
00:03:58,320 --> 00:04:01,240
any other number of things that we wish to add.

76
00:04:01,240 --> 00:04:05,640
So if I choose PowerShell 7, I get the PowerShell 7 option here,

77
00:04:05,640 --> 00:04:07,620
and I can then simply execute.

78
00:04:07,620 --> 00:04:10,560
So if I say cd and go into the Data folder,

79
00:04:10,560 --> 00:04:13,450
if I wanted to execute that script that's in there,

80
00:04:13,450 --> 00:04:14,770
same syntax again.

81
00:04:14,770 --> 00:04:16,120
Now you notice it comes back.

82
00:04:16,120 --> 00:04:18,220
It'll say, okay, unfortunately,

83
00:04:18,220 --> 00:04:22,750
it can't be loaded because running scripts has been disabled for PowerShell 7.

84
00:04:22,750 --> 00:04:24,240
So remember how we do this?

85
00:04:24,240 --> 00:04:29,790
Set‑ExecutionPolicy Unrestricted, and it comes back and it says,

86
00:04:29,790 --> 00:04:32,270
but the setting is overridden by another policy.

87
00:04:32,270 --> 00:04:36,730
So then we have to say, well, Get‑ExecutionPolicy,

88
00:04:36,730 --> 00:04:39,620
and we can see it's been defined as Restricted.

89
00:04:39,620 --> 00:04:42,010
So, but we know how to go through all of this,

90
00:04:42,010 --> 00:04:43,580
and we can change it as we need to.

91
00:04:43,580 --> 00:04:48,170
But you can run the scripts in exactly the same way by just executing them.

92
00:04:48,170 --> 00:04:51,230
Now, of course, mine's blocked here for that very reason.

93
00:04:51,230 --> 00:04:55,480
If I go back to the administrator one for Windows PowerShell,

94
00:04:55,480 --> 00:04:58,050
you can see that if I type Script on here,

95
00:04:58,050 --> 00:04:59,530
I can execute that script.

96
00:04:59,530 --> 00:05:02,540
So PowerShell 7 does have some extra security capabilities,

97
00:05:02,540 --> 00:05:05,140
which we already talked about earlier.

98
00:05:05,140 --> 00:05:06,910
Okay, so that's one option.

99
00:05:06,910 --> 00:05:10,180
But, when we were looking at building functions,

100
00:05:10,180 --> 00:05:13,920
it's not the easiest to be able to write a function,

101
00:05:13,920 --> 00:05:16,270
you know, to kind of go in and then say,

102
00:05:16,270 --> 00:05:21,260
Write‑Host, and then a message, and then do something else,

103
00:05:21,260 --> 00:05:22,190
and then close it off.

104
00:05:22,190 --> 00:05:25,140
And it kind of isn't the best interface for writing,

105
00:05:25,140 --> 00:05:27,960
which is why I really like using PowerShell ISE.

106
00:05:27,960 --> 00:05:32,850
Because if we were to do function Test, and do so,

107
00:05:32,850 --> 00:05:37,970
and then say Write‑Host, you can see I get IntelliSense as well.

108
00:05:37,970 --> 00:05:42,320
And I can say ‑ForegroundColor Green, and then I can close.

109
00:05:42,320 --> 00:05:44,360
Then I can execute the function here.

110
00:05:44,360 --> 00:05:50,020
And once I get underneath it, I can say Test, and boom, it works.

111
00:05:50,020 --> 00:05:52,660
And it's nice because it gives me a structured view.

112
00:05:52,660 --> 00:05:54,080
So what's our replacement, then?

113
00:05:54,080 --> 00:05:56,340
So clearly, it's not the Windows terminal.

114
00:05:56,340 --> 00:05:59,340
So what we're going to talk about is Visual Studio Code.

115
00:05:59,340 --> 00:06:01,580
So I'm going to right‑click here, choose Run as administrator.

116
00:06:01,580 --> 00:06:03,640
You can download this.

117
00:06:03,640 --> 00:06:08,280
It's actually a developer tool that's available for us to do things.

118
00:06:08,280 --> 00:06:11,030
I'm actually just going to drag this over a little bit here,

119
00:06:11,030 --> 00:06:13,640
and just close that,

120
00:06:13,640 --> 00:06:16,850
and get rid of that bottom section so I can show you what happens.

121
00:06:16,850 --> 00:06:21,150
Now, what we've got here is a code editor that's available to us.

122
00:06:21,150 --> 00:06:24,740
What I can do now is say File, New File,

123
00:06:24,740 --> 00:06:29,520
and let's go ahead and create a function called Show,

124
00:06:29,520 --> 00:06:35,740
let me just type Show‑Message,

125
00:06:35,740 --> 00:06:50,840
and we'll do very simple Write‑Host "This is a message" like so,

126
00:06:50,840 --> 00:07:04,260
and we'll say ‑‑‑ let me do ‑‑‑ Okay, so now we have that.

127
00:07:04,260 --> 00:07:09,560
I can say File, Save As, and then it's going to ask me where I wish to save.

128
00:07:09,560 --> 00:07:15,160
So I'm going to go to my Data folder, and I'll call this function.ps1.

129
00:07:15,160 --> 00:07:20,270
And I can go and change it in here, so you can see the list of options,

130
00:07:20,270 --> 00:07:21,060
so PowerShell.

131
00:07:21,060 --> 00:07:23,740
So function, I'll save it as that.

132
00:07:23,740 --> 00:07:28,290
And once that's done, what I can then do is go onto the section here,

133
00:07:28,290 --> 00:07:30,450
right‑click, and say Run Selection.

134
00:07:30,450 --> 00:07:31,220
And then, of course,

135
00:07:31,220 --> 00:07:35,520
what it will do is tell me that my PowerShell is actually starting up.

136
00:07:35,520 --> 00:07:39,520
So I can say switch to a different session or restart the current session.

137
00:07:39,520 --> 00:07:40,860
So I'm going to do that.

138
00:07:40,860 --> 00:07:49,040
So you can see, let me just say A for always run.

139
00:07:49,040 --> 00:07:54,140
Okay, let me get rid of the bottom section,

140
00:07:54,140 --> 00:07:57,940
and then I can select that there, and then say Run Selection.

141
00:07:57,940 --> 00:07:59,680
Okay, so it's now created that.

142
00:07:59,680 --> 00:08:03,790
I can then say Show‑Message, and it will render the message for me.

143
00:08:03,790 --> 00:08:06,550
So, very similar to the PowerShell ISE,

144
00:08:06,550 --> 00:08:10,580
except what I can do is I have this rich interface of

145
00:08:10,580 --> 00:08:15,200
where I can kind of run a single line, check a specific variable.

146
00:08:15,200 --> 00:08:17,750
I can pass values backwards and forwards.

147
00:08:17,750 --> 00:08:23,490
I can also, then see any problems that may have occurred in the code itself.

148
00:08:23,490 --> 00:08:33,080
So, for example, if I had a variable here and I had some values inside of it,

149
00:08:33,080 --> 00:08:38,240
and then I wanted to kind of get that variable,

150
00:08:38,240 --> 00:08:41,990
pipe it out, and then say Select, you know,

151
00:08:41,990 --> 00:08:47,260
the first one, for example, then what it does when it starts to check the file,

152
00:08:47,260 --> 00:08:50,820
it will actually look at that one, and IntelliSense will run.

153
00:08:50,820 --> 00:08:53,120
So I'm going to put it back to the output in the debug console,

154
00:08:53,120 --> 00:08:54,490
for example, or the terminal.

155
00:08:54,490 --> 00:08:57,180
I could then leave that to run,

156
00:08:57,180 --> 00:09:00,430
and PowerShell would automatically identify a problem.

157
00:09:00,430 --> 00:09:04,410
So, for example, it will automatically say you can't have the word Select there.

158
00:09:04,410 --> 00:09:06,940
It should be Select object, for example.

159
00:09:06,940 --> 00:09:12,050
So, it's a nice, powerful interface for actually writing and executing scripts.

160
00:09:12,050 --> 00:09:15,230
Now if I wanted to execute the specific script that we've got,

161
00:09:15,230 --> 00:09:19,460
I can say Open File, and then I can open the script that I've written before.

162
00:09:19,460 --> 00:09:21,140
You'll see it's here.

163
00:09:21,140 --> 00:09:26,480
I can then simply just go here and say Run, and this will run that for me.

164
00:09:26,480 --> 00:09:29,060
Now, of course, it comes back and complains because of security.

165
00:09:29,060 --> 00:09:30,520
And why does it complain?

166
00:09:30,520 --> 00:09:33,040
Because it has a signature associated to it.

167
00:09:33,040 --> 00:09:35,590
But if I wanted to run the individual piece,

168
00:09:35,590 --> 00:09:38,590
I can run that and then run Write‑Message here,

169
00:09:38,590 --> 00:09:41,440
and then I'm able to execute it as you would normally.

170
00:09:41,440 --> 00:09:44,100
So the signature is the bit that causes a security issue,

171
00:09:44,100 --> 00:09:45,640
but that's perfectly fine.

172
00:09:45,640 --> 00:09:47,060
But, as you can see, it straightforward.

173
00:09:47,060 --> 00:09:53,000
It lets you kind of run the PowerShell script directly in the top section to the bottom section.

