1
00:00:01,140 --> 00:00:04,090
Alrighty, it's demo time. Let's kick off

2
00:00:04,090 --> 00:00:05,880
by taking a look at the paths PowerShell

3
00:00:05,880 --> 00:00:08,370
will look in to find any modules that have

4
00:00:08,370 --> 00:00:10,360
been installed which are stored in that

5
00:00:10,360 --> 00:00:12,430
environment variable we just spoke about,

6
00:00:12,430 --> 00:00:15,930
PSModulePath. Over here in the PowerShell

7
00:00:15,930 --> 00:00:21,060
7 console, if I run $env:PSModulePath to

8
00:00:21,060 --> 00:00:22,980
take a look at the current value, we've

9
00:00:22,980 --> 00:00:24,650
got a few folders in here that are

10
00:00:24,650 --> 00:00:27,780
separated by a semi‑colon. Let's run that

11
00:00:27,780 --> 00:00:30,600
again, but use the split operator to split

12
00:00:30,600 --> 00:00:33,110
that value on the semi‑colon. There we go,

13
00:00:33,110 --> 00:00:35,320
that's a bit easier to read. These are the

14
00:00:35,320 --> 00:00:37,870
paths to the locations of the modules that

15
00:00:37,870 --> 00:00:40,490
are installed on disk, and PowerShell uses

16
00:00:40,490 --> 00:00:43,460
these paths in order to locate modules

17
00:00:43,460 --> 00:00:45,600
when you don't specify the full path to a

18
00:00:45,600 --> 00:00:47,750
module. Given we're working with

19
00:00:47,750 --> 00:00:50,170
PowerShell 7 here, just a quick sidebar on

20
00:00:50,170 --> 00:00:52,460
something I want you to be aware of. By

21
00:00:52,460 --> 00:00:55,120
default, Windows PowerShell and PowerShell

22
00:00:55,120 --> 00:00:58,640
7 store modules in different locations.

23
00:00:58,640 --> 00:01:01,490
PowerShell 7 combines these locations in

24
00:01:01,490 --> 00:01:04,660
the PSModulePath. environment variable. So

25
00:01:04,660 --> 00:01:06,430
if you looked at this default environment

26
00:01:06,430 --> 00:01:08,440
variable in Windows PowerShell, it

27
00:01:08,440 --> 00:01:10,740
wouldn't list these two folders, but

28
00:01:10,740 --> 00:01:13,320
PowerShell 7 automatically combines all of

29
00:01:13,320 --> 00:01:15,490
these paths into the variable to make it

30
00:01:15,490 --> 00:01:17,570
easier for you to migrate across from

31
00:01:17,570 --> 00:01:20,200
Windows PowerShell. You can add a path

32
00:01:20,200 --> 00:01:22,250
into this environment variable to tell

33
00:01:22,250 --> 00:01:24,030
PowerShell to search for modules in

34
00:01:24,030 --> 00:01:26,640
another location on your machine. Let's

35
00:01:26,640 --> 00:01:29,240
say, for example, I had a folder in the C

36
00:01:29,240 --> 00:01:32,350
Drive named MyPowerShellModules. You can

37
00:01:32,350 --> 00:01:37,680
add this path by running $env:PSModulePath

38
00:01:37,680 --> 00:01:42,370
=, then repeat $env:PSModulePath,

39
00:01:42,370 --> 00:01:44,940
indicating that we want the current value

40
00:01:44,940 --> 00:01:47,090
plus my additional path of

41
00:01:47,090 --> 00:01:51,500
C:\MyPowerShellModules. Note there that I

42
00:01:51,500 --> 00:01:54,130
have also put a semi‑colon at the start of

43
00:01:54,130 --> 00:01:56,060
that, because I'm adding this value to a

44
00:01:56,060 --> 00:01:58,190
string of paths separated by the

45
00:01:58,190 --> 00:02:01,300
semi‑colon. If I up arrow and rerun the

46
00:02:01,300 --> 00:02:03,550
value splitting it on the semi‑colon,

47
00:02:03,550 --> 00:02:04,540
you'll see the path to

48
00:02:04,540 --> 00:02:08,350
C:\MyPowerShellModules is now added to the

49
00:02:08,350 --> 00:02:11,420
list. Now, that is only persistent for

50
00:02:11,420 --> 00:02:14,010
this PowerShell session. If I close

51
00:02:14,010 --> 00:02:16,970
PowerShell and then relaunch it, and then

52
00:02:16,970 --> 00:02:18,520
look at the value of that environment

53
00:02:18,520 --> 00:02:21,120
variable again, actually let's split it on

54
00:02:21,120 --> 00:02:23,260
that semi‑colon to make it easier to see,

55
00:02:23,260 --> 00:02:25,100
you will notice we no longer have our

56
00:02:25,100 --> 00:02:27,920
custom path listed. There are a couple of

57
00:02:27,920 --> 00:02:30,400
ways we can make sure that the value of

58
00:02:30,400 --> 00:02:32,740
PSModulePath has our custom folder in

59
00:02:32,740 --> 00:02:35,770
every session. The first way is to add the

60
00:02:35,770 --> 00:02:38,250
previous command to a PowerShell profile,

61
00:02:38,250 --> 00:02:40,730
so every time PowerShell is launched it

62
00:02:40,730 --> 00:02:43,470
will amend that variable. Another way is

63
00:02:43,470 --> 00:02:45,180
to make a permanent change to the

64
00:02:45,180 --> 00:02:46,890
environment variable in the operating

65
00:02:46,890 --> 00:02:49,180
system. Now, I could show you how to do

66
00:02:49,180 --> 00:02:51,430
that using the Windows interface, but,

67
00:02:51,430 --> 00:02:53,650
hey, this is a PowerShell course, so let

68
00:02:53,650 --> 00:02:55,100
me show you how to do that using

69
00:02:55,100 --> 00:02:57,910
PowerShell. I'll start by creating a new

70
00:02:57,910 --> 00:03:00,670
variable named currentpath, and for the

71
00:03:00,670 --> 00:03:02,290
value I'm going to use the

72
00:03:02,290 --> 00:03:05,190
GetEnvironmentVariable method, and I want

73
00:03:05,190 --> 00:03:07,140
to get the environment variable named

74
00:03:07,140 --> 00:03:10,190
PSModulePath in the context of the

75
00:03:10,190 --> 00:03:12,610
Machine. Let's take a look at the value of

76
00:03:12,610 --> 00:03:15,130
that currentpath variable, and split it on

77
00:03:15,130 --> 00:03:17,320
the semi‑colon. We've got a couple of

78
00:03:17,320 --> 00:03:19,180
paths returned there that we just saw in

79
00:03:19,180 --> 00:03:21,760
the previous example. We're not seeing all

80
00:03:21,760 --> 00:03:23,190
of the paths that we did a few minutes

81
00:03:23,190 --> 00:03:25,560
ago, because not all of them are stored in

82
00:03:25,560 --> 00:03:28,540
the Machine environment variable context.

83
00:03:28,540 --> 00:03:30,870
I'll create a new PowerShell variable

84
00:03:30,870 --> 00:03:34,770
named $newpath, and for the value I want

85
00:03:34,770 --> 00:03:37,080
those entries for the currentpath, and

86
00:03:37,080 --> 00:03:39,430
similar to before, I'm going to add in the

87
00:03:39,430 --> 00:03:43,120
value for C:\MyPowerShellModules. Again,

88
00:03:43,120 --> 00:03:45,770
note that I'm dropping a semi‑colon at the

89
00:03:45,770 --> 00:03:47,820
start because we're adding this to a

90
00:03:47,820 --> 00:03:51,140
string already separated by semi‑colons.

91
00:03:51,140 --> 00:03:53,570
To add our custom path, we can use the

92
00:03:53,570 --> 00:03:56,190
SetEnvironmentVariable method, noting that

93
00:03:56,190 --> 00:03:58,060
we want to set the PSModulePath

94
00:03:58,060 --> 00:04:00,530
environment variable, and the value we

95
00:04:00,530 --> 00:04:03,530
want to set is $newpath, which contains

96
00:04:03,530 --> 00:04:06,320
the semi‑colon‑separated string of paths,

97
00:04:06,320 --> 00:04:08,870
including our custom path, and we're

98
00:04:08,870 --> 00:04:11,010
setting the Machine environment variable.

99
00:04:11,010 --> 00:04:13,920
In PowerShell, no news is usually good

100
00:04:13,920 --> 00:04:16,340
news, but if I up arrow back to retrieving

101
00:04:16,340 --> 00:04:18,740
the value using the Get method, and then

102
00:04:18,740 --> 00:04:21,000
split that on the semi‑colon, you'll see

103
00:04:21,000 --> 00:04:23,440
our custom path is now listed and will be

104
00:04:23,440 --> 00:04:26,490
used by PowerShell. This time, if I close

105
00:04:26,490 --> 00:04:29,640
the PowerShell session and then reopen it,

106
00:04:29,640 --> 00:04:31,670
and then check out the PSModulePath

107
00:04:31,670 --> 00:04:34,300
variable, in a brand‑new session you'll

108
00:04:34,300 --> 00:04:36,190
see the custom path is still listed,

109
00:04:36,190 --> 00:04:38,060
because it's now set in the Windows

110
00:04:38,060 --> 00:04:41,240
environment variable. To finish up here,

111
00:04:41,240 --> 00:04:43,220
I'm going to run the command Get‑Module

112
00:04:43,220 --> 00:04:45,470
with no parameters, and we'll see what

113
00:04:45,470 --> 00:04:47,100
modules have been loaded into this

114
00:04:47,100 --> 00:04:49,760
specific PowerShell session. We've got a

115
00:04:49,760 --> 00:04:51,710
couple there that's always loaded when

116
00:04:51,710 --> 00:04:54,070
PowerShell is launched. I want to repeat

117
00:04:54,070 --> 00:04:56,630
that so it's clear. Get‑Module with no

118
00:04:56,630 --> 00:04:58,790
parameters will show the modules that are

119
00:04:58,790 --> 00:05:01,420
currently loaded into this session. It

120
00:05:01,420 --> 00:05:03,580
doesn't return a list of all modules that

121
00:05:03,580 --> 00:05:05,820
are installed on this computer. To do

122
00:05:05,820 --> 00:05:08,280
that, you can run Get‑Module with the

123
00:05:08,280 --> 00:05:11,280
ListAvailable parameter. And if I scroll

124
00:05:11,280 --> 00:05:13,810
up to the top, check this out, these are

125
00:05:13,810 --> 00:05:15,470
the paths that are specified in the

126
00:05:15,470 --> 00:05:18,190
PSModulePath environment variable, and

127
00:05:18,190 --> 00:05:19,990
PowerShell has gone and looked in each of

128
00:05:19,990 --> 00:05:22,050
those paths to see if it can find any

129
00:05:22,050 --> 00:05:24,730
modules. And as you can see, it's found a

130
00:05:24,730 --> 00:05:26,940
heap that are installed by default.

131
00:05:26,940 --> 00:05:29,290
PowerShell will only be looking in paths

132
00:05:29,290 --> 00:05:30,930
that are stored in our PSModulePath

133
00:05:30,930 --> 00:05:33,820
variable. If we have PowerShell modules on

134
00:05:33,820 --> 00:05:35,670
our system that aren't stored in one of

135
00:05:35,670 --> 00:05:42,000
those paths, neither Get‑Module nor Get‑Module ListAvailable will locate them.

