1
00:00:00,140 --> 00:00:04,740
So let's talk about the steps to creating a PowerShell script.

2
00:00:04,740 --> 00:00:09,030
The first thing is to determine the editor that we wish to utilize.

3
00:00:09,030 --> 00:00:10,440
Now, all through the course,

4
00:00:10,440 --> 00:00:14,320
we've been using the Windows PowerShell terminal and console,

5
00:00:14,320 --> 00:00:16,380
so that is always the first choice.

6
00:00:16,380 --> 00:00:18,460
You've got the Windows PowerShell console.

7
00:00:18,460 --> 00:00:19,730
It's on Windows.

8
00:00:19,730 --> 00:00:21,740
You can just open it and use it.

9
00:00:21,740 --> 00:00:24,230
We also have the Windows Terminal, which we've been using,

10
00:00:24,230 --> 00:00:27,310
which is the new version that's available to us.

11
00:00:27,310 --> 00:00:29,700
And then, of course, we also have Visual Studio Code,

12
00:00:29,700 --> 00:00:31,840
which I'm quite fond of,

13
00:00:31,840 --> 00:00:35,920
an application that isn't really necessarily used for creating PowerShell

14
00:00:35,920 --> 00:00:41,040
necessarily, but it gives a richer interface to building them.

15
00:00:41,040 --> 00:00:42,790
Now when we're looking at building the scripts,

16
00:00:42,790 --> 00:00:45,450
we have a structure that we need to look at building.

17
00:00:45,450 --> 00:00:50,110
So first off, you define any required modules or snap‑ins.

18
00:00:50,110 --> 00:00:54,910
So, for example, if I was working with server architecture on‑premises,

19
00:00:54,910 --> 00:00:56,510
for example, like Active Directory,

20
00:00:56,510 --> 00:00:59,640
I would have to import the Active Directory module

21
00:00:59,640 --> 00:01:02,440
as part of my PowerShell script.

22
00:01:02,440 --> 00:01:03,080
Then, of course,

23
00:01:03,080 --> 00:01:08,220
we declare any variables. So these are globalized variables that need values,

24
00:01:08,220 --> 00:01:11,360
whether they need user input or not. And then, of course,

25
00:01:11,360 --> 00:01:12,790
you define the functions,

26
00:01:12,790 --> 00:01:14,880
which will be the reusable blocks that we're going to

27
00:01:14,880 --> 00:01:17,840
execute throughout the process.

28
00:01:17,840 --> 00:01:19,320
So to create a PowerShell script,

29
00:01:19,320 --> 00:01:22,920
we first launch the selected editor of choice, so

30
00:01:22,920 --> 00:01:26,440
the console or Visual Studio, etc.

31
00:01:26,440 --> 00:01:30,640
You then add the PowerShell code that you want to build.

32
00:01:30,640 --> 00:01:37,040
Then you digitally sign if you're distributing it or you need better security.

33
00:01:37,040 --> 00:01:44,340
You save the file as a something .ps1, and that becomes your PowerShell file.

34
00:01:44,340 --> 00:01:47,540
Now one of the most important things is to let either

35
00:01:47,540 --> 00:01:50,170
other users that may be using the PowerShell script or

36
00:01:50,170 --> 00:01:51,950
even to remind yourself. Trust me,

37
00:01:51,950 --> 00:01:55,750
it's really important sometimes when you look back at PowerShell scripts that

38
00:01:55,750 --> 00:01:58,540
you've written that you often wonder what they're doing.

39
00:01:58,540 --> 00:02:01,360
It's good to comment them. So we have a couple of

40
00:02:01,360 --> 00:02:03,110
different ways of commenting the code.

41
00:02:03,110 --> 00:02:05,530
We can either do what's called single‑line PowerShell comments.

42
00:02:05,530 --> 00:02:10,170
So these normally begin with the number or hash character, and

43
00:02:10,170 --> 00:02:16,040
everything is on the same line, and basically, it gets ignored by PowerShell.

44
00:02:16,040 --> 00:02:19,880
The next one would be block comments or multiline comments.

45
00:02:19,880 --> 00:02:25,340
So you prefix this with an open and closing, so it's the chevron with

46
00:02:25,340 --> 00:02:29,100
a hash and then a hash with a chevron, and then you can write

47
00:02:29,100 --> 00:02:33,040
multiline comments into the PowerShell script.

48
00:02:33,040 --> 00:02:36,650
You also can then create what's called comment‑based help.

49
00:02:36,650 --> 00:02:40,620
So this is a collection of keywords and string values that get wrapped

50
00:02:40,620 --> 00:02:45,300
within a block comment, so using the same kind of chevron hash, but

51
00:02:45,300 --> 00:02:47,270
it's one that's defined out of the box,

52
00:02:47,270 --> 00:02:50,790
and you can add specific property values so that when

53
00:02:50,790 --> 00:02:53,220
someone types in your function name,

54
00:02:53,220 --> 00:02:58,540
they can do ‑Help, and it will return those values automatically.

55
00:02:58,540 --> 00:03:00,730
So what does commenting code look like?

56
00:03:00,730 --> 00:03:01,150
Well,

57
00:03:01,150 --> 00:03:04,150
if we look at the single line commenting, you can see I've got

58
00:03:04,150 --> 00:03:06,700
a standard function called Invoke‑Message,

59
00:03:06,700 --> 00:03:10,120
and it's going to write a simple message. To comment

60
00:03:10,120 --> 00:03:11,800
it, you can see the green line here,

61
00:03:11,800 --> 00:03:16,760
so # This function returns a simple string. That's literally the

62
00:03:16,760 --> 00:03:22,480
single line commenting. Block commenting obviously uses the chevron

63
00:03:22,480 --> 00:03:25,450
and the hashes to create a multiline version.

64
00:03:25,450 --> 00:03:26,350
So once again,

65
00:03:26,350 --> 00:03:31,430
I've got my same Invoke‑Message function, except this time it writes it in red.

66
00:03:31,430 --> 00:03:35,410
So I have the same text, This function returns a simple string.

67
00:03:35,410 --> 00:03:38,850
Next line, The string will be displayed in red. So it means

68
00:03:38,850 --> 00:03:43,540
that when someone reads the PowerShell, they know exactly what's going on.

69
00:03:43,540 --> 00:03:47,280
Then, of course, we can also comment out existing code.

70
00:03:47,280 --> 00:03:48,650
So there's my function.

71
00:03:48,650 --> 00:03:50,670
Let's say I don't want to use that anymore.

72
00:03:50,670 --> 00:03:55,640
I can simply put the hash or the number symbol right at the beginning of

73
00:03:55,640 --> 00:03:58,790
where it says function and any of the lines underneath,

74
00:03:58,790 --> 00:04:02,040
and it will change that to be commented out.

75
00:04:02,040 --> 00:04:04,480
So there are times when you're building a PowerShell script where you

76
00:04:04,480 --> 00:04:06,440
create a function, you don't want to use it anymore,

77
00:04:06,440 --> 00:04:12,000
and you want to make sure it's not going to execute. You can simply commentate that block of code.

