1
00:00:00,940 --> 00:00:03,450
Now let's explore how modules get imported

2
00:00:03,450 --> 00:00:05,620
into your PowerShell session, both

3
00:00:05,620 --> 00:00:08,590
explicitly and also by using the auto‑load

4
00:00:08,590 --> 00:00:11,340
feature. There's a number of modules

5
00:00:11,340 --> 00:00:13,770
installed on a Windows system by default

6
00:00:13,770 --> 00:00:15,960
out of the box, and I'd like to spend a

7
00:00:15,960 --> 00:00:18,110
few minutes working with those before we

8
00:00:18,110 --> 00:00:19,780
look at installing and managing

9
00:00:19,780 --> 00:00:22,640
non‑default modules. You can use the

10
00:00:22,640 --> 00:00:25,870
command Import‑Module to explicitly import

11
00:00:25,870 --> 00:00:28,680
a module into this session. But before we

12
00:00:28,680 --> 00:00:31,010
do, let's check out the help quickly by

13
00:00:31,010 --> 00:00:35,140
running Get‑Help Import‑Module. You'll see

14
00:00:35,140 --> 00:00:37,470
here in the first syntax block that the

15
00:00:37,470 --> 00:00:39,550
only mandatory parameter we need to

16
00:00:39,550 --> 00:00:42,160
specify for the Import‑Module command to

17
00:00:42,160 --> 00:00:45,280
run is ‑Name. And additionally, it is

18
00:00:45,280 --> 00:00:47,570
positional, so we aren't required to use

19
00:00:47,570 --> 00:00:49,650
the name parameter, but we will to start

20
00:00:49,650 --> 00:00:51,940
with. Seems reasonably straightforward,

21
00:00:51,940 --> 00:00:55,190
right? Before I import a module, if I run

22
00:00:55,190 --> 00:00:57,290
Get‑Module, you'll see the modules that

23
00:00:57,290 --> 00:00:59,870
are imported into our current session. And

24
00:00:59,870 --> 00:01:01,710
remember, as we discussed in the last

25
00:01:01,710 --> 00:01:03,980
clip, this only shows us modules that have

26
00:01:03,980 --> 00:01:05,910
bean imported into this particular

27
00:01:05,910 --> 00:01:09,010
session. So let's import a module, and

28
00:01:09,010 --> 00:01:10,930
I'll do that by running Import‑Module

29
00:01:10,930 --> 00:01:14,170
‑Name BitsTransfer. That's all the help

30
00:01:14,170 --> 00:01:16,090
said that I need to specify, so we should

31
00:01:16,090 --> 00:01:19,140
be good. If I run Get‑Module again, you'll

32
00:01:19,140 --> 00:01:21,180
see that the BitsTransfer module is now

33
00:01:21,180 --> 00:01:23,290
loaded into this session and is ready for

34
00:01:23,290 --> 00:01:26,300
use. Now there's a command that I happen

35
00:01:26,300 --> 00:01:29,710
to know about named Get‑SmbShare that is

36
00:01:29,710 --> 00:01:32,010
not included in any of these modules that

37
00:01:32,010 --> 00:01:34,140
have been imported into this session so

38
00:01:34,140 --> 00:01:36,780
far. It is a command that's a part of a

39
00:01:36,780 --> 00:01:40,100
module named SMBShare. Because of the

40
00:01:40,100 --> 00:01:41,830
auto‑loading feature we spoke about

41
00:01:41,830 --> 00:01:44,530
previously, I do have the ability to just

42
00:01:44,530 --> 00:01:47,330
run the command, and, as the SMBShare

43
00:01:47,330 --> 00:01:49,620
module is sitting in one of those PS

44
00:01:49,620 --> 00:01:52,300
module paths, PowerShell will go and find

45
00:01:52,300 --> 00:01:54,420
which module the command is a part of and

46
00:01:54,420 --> 00:01:57,120
then import the module for me. Let's try

47
00:01:57,120 --> 00:02:00,130
it out. I'll run Get‑SmbShare, and the

48
00:02:00,130 --> 00:02:02,040
command executed, and I can see a list of

49
00:02:02,040 --> 00:02:04,600
shares on this machine. If I run

50
00:02:04,600 --> 00:02:06,490
Get‑Module again, you will notice that the

51
00:02:06,490 --> 00:02:09,330
module named SmbShare has automatically

52
00:02:09,330 --> 00:02:11,630
been loaded into my PowerShell session

53
00:02:11,630 --> 00:02:13,890
because I used a command that is included

54
00:02:13,890 --> 00:02:16,900
in that module. In a nutshell, that's how

55
00:02:16,900 --> 00:02:20,280
the auto‑load feature works. Again, let me

56
00:02:20,280 --> 00:02:22,400
just reiterate so this really hits home.

57
00:02:22,400 --> 00:02:25,120
Auto‑load won't work if the module is not

58
00:02:25,120 --> 00:02:27,590
sitting in one of the paths included in

59
00:02:27,590 --> 00:02:30,380
the PS module path variable. I hope with

60
00:02:30,380 --> 00:02:32,240
these examples, you've seen the power of

61
00:02:32,240 --> 00:02:34,920
having modules in the PS module path and

62
00:02:34,920 --> 00:02:38,010
the benefits it can provide. With that

63
00:02:38,010 --> 00:02:40,280
said, there may be times where you don't

64
00:02:40,280 --> 00:02:42,120
have the module stored in one of those

65
00:02:42,120 --> 00:02:44,890
paths, and you just want to load it in ad

66
00:02:44,890 --> 00:02:46,780
hoc from somewhere else on the file

67
00:02:46,780 --> 00:02:49,860
system. This can be achieved by specifying

68
00:02:49,860 --> 00:02:52,120
the full path to the folder that contains

69
00:02:52,120 --> 00:02:54,210
the module when running the Import‑Module

70
00:02:54,210 --> 00:02:56,580
command. You might want to do this if

71
00:02:56,580 --> 00:02:58,850
you're doing some testing of a development

72
00:02:58,850 --> 00:03:00,790
or test module and you just want to

73
00:03:00,790 --> 00:03:03,230
quickly load it into your session. I've

74
00:03:03,230 --> 00:03:05,110
got a web browser here, and I'm on a

75
00:03:05,110 --> 00:03:06,930
Github page that is the home for a

76
00:03:06,930 --> 00:03:09,340
PowerShell module created by a good friend

77
00:03:09,340 --> 00:03:11,780
of mine that allows you to query a popular

78
00:03:11,780 --> 00:03:14,140
data breach service named Have I Been

79
00:03:14,140 --> 00:03:16,700
Pwned, which, as an aside, is a great

80
00:03:16,700 --> 00:03:18,770
service created by another Pluralsight

81
00:03:18,770 --> 00:03:21,740
author named Troy Hunt. The PowerShell

82
00:03:21,740 --> 00:03:24,940
module itself is named Have I Been Pwned.

83
00:03:24,940 --> 00:03:26,850
Now this module is actually on the

84
00:03:26,850 --> 00:03:28,860
PowerShell Gallery, so I don't need to

85
00:03:28,860 --> 00:03:31,120
come to Github to get this module, and

86
00:03:31,120 --> 00:03:32,890
I'll show you in the next module in this

87
00:03:32,890 --> 00:03:35,410
course how to install from the Gallery.

88
00:03:35,410 --> 00:03:37,750
But I'm just using this as an example of

89
00:03:37,750 --> 00:03:39,960
maybe getting a dev or test branch of a

90
00:03:39,960 --> 00:03:42,460
module from somewhere like Github that you

91
00:03:42,460 --> 00:03:45,540
might want to try or test out. If I click

92
00:03:45,540 --> 00:03:47,580
on this code icon at the top, I can

93
00:03:47,580 --> 00:03:49,830
download a zip of this code repository

94
00:03:49,830 --> 00:03:52,620
down to my computer. Let's open that

95
00:03:52,620 --> 00:03:55,290
download location, and I'll unblock the

96
00:03:55,290 --> 00:03:58,670
file in the properties and then extract it

97
00:03:58,670 --> 00:04:00,690
and put it in the root of this downloads

98
00:04:00,690 --> 00:04:03,830
folder. I'll just rename that extracted

99
00:04:03,830 --> 00:04:08,030
folder to remove the ‑master. So now we've

100
00:04:08,030 --> 00:04:10,960
got this folder named haveibeenpwned and

101
00:04:10,960 --> 00:04:13,510
inside of that are all the script files

102
00:04:13,510 --> 00:04:15,750
that make up this module. I don't want to

103
00:04:15,750 --> 00:04:18,040
go too far into this, but let's take a

104
00:04:18,040 --> 00:04:20,250
look inside of this public folder and

105
00:04:20,250 --> 00:04:21,960
you'll see all of the commands for this

106
00:04:21,960 --> 00:04:24,550
module are stored there as discreet script

107
00:04:24,550 --> 00:04:26,850
files. So that's just a quick under the

108
00:04:26,850 --> 00:04:29,240
covers look at what it actually looks like

109
00:04:29,240 --> 00:04:32,540
inside of a module folder. Okay, so now

110
00:04:32,540 --> 00:04:34,860
we've got a PowerShell module in a path

111
00:04:34,860 --> 00:04:37,280
that is not listed in the PS module path

112
00:04:37,280 --> 00:04:39,360
variable because it's currently in my

113
00:04:39,360 --> 00:04:41,810
downloads folder. Let's go back to the

114
00:04:41,810 --> 00:04:44,090
PowerShell console and try doing an

115
00:04:44,090 --> 00:04:47,210
Import‑Module ‑Name haveibeenpwned, and

116
00:04:47,210 --> 00:04:49,390
we'll see what happens. And you'll notice

117
00:04:49,390 --> 00:04:51,750
it fails because PowerShell wasn't able to

118
00:04:51,750 --> 00:04:54,330
find a valid module file in any module

119
00:04:54,330 --> 00:04:57,360
directory. But I can explicitly import the

120
00:04:57,360 --> 00:04:59,880
module into this PowerShell session by

121
00:04:59,880 --> 00:05:02,610
running Import‑Module ‑Name and then

122
00:05:02,610 --> 00:05:04,500
specify the full path to the module

123
00:05:04,500 --> 00:05:06,950
folder, which in this case, is

124
00:05:06,950 --> 00:05:10,280
C:\Users\Matt\Downloads\haveibeenpwned.

125
00:05:10,280 --> 00:05:16,020
I'll run Get‑Module again and you can see

126
00:05:16,020 --> 00:05:18,640
the haveibeenpwned module is now imported

127
00:05:18,640 --> 00:05:20,720
into this session, and the commands are

128
00:05:20,720 --> 00:05:23,720
available for me to start using. Finally,

129
00:05:23,720 --> 00:05:25,460
let's explore one of the use cases we

130
00:05:25,460 --> 00:05:27,690
spoke about in the earlier theory clip

131
00:05:27,690 --> 00:05:30,310
where you can use Import‑Module to define

132
00:05:30,310 --> 00:05:32,740
a prefix for all commands included in a

133
00:05:32,740 --> 00:05:35,320
module. You may want to do this to avoid

134
00:05:35,320 --> 00:05:37,410
conflicts between commands in different

135
00:05:37,410 --> 00:05:40,490
modules that have the same name. I know of

136
00:05:40,490 --> 00:05:43,260
a default module named Scheduled Tasks, so

137
00:05:43,260 --> 00:05:46,470
let's run Import‑Module ScheduledTask and

138
00:05:46,470 --> 00:05:49,070
I'll specify the prefix parameter and

139
00:05:49,070 --> 00:05:52,740
we'll use a prefix of PS for Pluralsight.

140
00:05:52,740 --> 00:05:54,830
If I run get module, you'll notice the

141
00:05:54,830 --> 00:05:57,600
module has been imported successfully. And

142
00:05:57,600 --> 00:05:59,630
if you look closely at the commands, they

143
00:05:59,630 --> 00:06:02,840
all have a prefix of PS. So normally this

144
00:06:02,840 --> 00:06:04,550
first command would simply be

145
00:06:04,550 --> 00:06:06,930
Disabled‑ScheduledTask, but due to us

146
00:06:06,930 --> 00:06:09,350
importing the module with a prefix, to run

147
00:06:09,350 --> 00:06:15,000
this command now, we would need to run Disabled‑PSScheduledTask.

