1
00:00:01,640 --> 00:00:05,220
The environment provider has numerous built‑in variables that stores

2
00:00:05,220 --> 00:00:08,640
information about the operating system environment.

3
00:00:08,640 --> 00:00:13,840
These information are accessed by OS, scripts, and programs.

4
00:00:13,840 --> 00:00:17,200
These variables provide information like the Windows directory,

5
00:00:17,200 --> 00:00:20,940
OS installation path, temporary directory path,

6
00:00:20,940 --> 00:00:26,340
user's OneDrive, and many such details, which we will look further in demo.

7
00:00:26,340 --> 00:00:29,640
Almost all these variables are self‑explanatory,

8
00:00:29,640 --> 00:00:32,670
and we won't feel the need to emphasize much on each

9
00:00:32,670 --> 00:00:34,840
and every environment variable.

10
00:00:34,840 --> 00:00:40,240
We'll try to cover most of them and especially the ones that are used widely.

11
00:00:40,240 --> 00:00:44,240
So now, we will quickly jump into our demo environment.

12
00:00:44,240 --> 00:00:49,040
In this demo, we will look into the significance of environment variables.

13
00:00:49,040 --> 00:00:53,920
We will then see how to extract values out of these environment variables.

14
00:00:53,920 --> 00:00:57,540
To list all the environment variables.

15
00:00:57,540 --> 00:01:01,590
I will run Get‑ChildItem and enter env:\ drive.

16
00:01:01,590 --> 00:01:06,140
On the left side, we have the list of environment variables,

17
00:01:06,140 --> 00:01:10,840
and on the right side, we have the values that are stored in those variables.

18
00:01:10,840 --> 00:01:16,740
The first one is ALLUSERSPROFILE that points to c:\ProgramData folder.

19
00:01:16,740 --> 00:01:21,340
The second one is APPDATA that points to Users, Roaming folder.

20
00:01:21,340 --> 00:01:25,940
So, if in a script, I want to change my directory to this path,

21
00:01:25,940 --> 00:01:29,570
I can easily mention ALLUSERSPROFILE variable instead of

22
00:01:29,570 --> 00:01:32,340
typing the entire Roaming folder path.

23
00:01:32,340 --> 00:01:35,510
We then have ProgramFiles variable that provides

24
00:01:35,510 --> 00:01:38,440
details about ProgramFiles folders.

25
00:01:38,440 --> 00:01:42,670
Next is one of the widely used variable COMPUTERNAME. That

26
00:01:42,670 --> 00:01:45,240
provides the hostname of the system.

27
00:01:45,240 --> 00:01:49,900
Imagine you are extracting some data from hundreds of computers and

28
00:01:49,900 --> 00:01:52,940
exporting their data in their respective text file.

29
00:01:52,940 --> 00:01:54,950
If you bring all those files together,

30
00:01:54,950 --> 00:01:57,540
how will you differentiate between each file?

31
00:01:57,540 --> 00:02:02,040
The solution to this is that you can use COMPUTERNAME variable in the script,

32
00:02:02,040 --> 00:02:06,940
extract the hostname, and save the text file with the hostname in it.

33
00:02:06,940 --> 00:02:09,540
This would look something like this.

34
00:02:09,540 --> 00:02:12,320
The ComSpec variable specifies and points to the

35
00:02:12,320 --> 00:02:14,750
command‑line interpreter of the OS.

36
00:02:14,750 --> 00:02:19,010
DriverData variable gives information about DriverData folder path.

37
00:02:19,010 --> 00:02:21,170
The HOMEDRIVE, HOMEPATH,

38
00:02:21,170 --> 00:02:25,490
and LOCALAPPDATA variables points to the Users home drive,

39
00:02:25,490 --> 00:02:29,330
Users home directory, and AppData\Local folder.

40
00:02:29,330 --> 00:02:33,420
We then have LOGONSERVER variable that basically tells us which

41
00:02:33,420 --> 00:02:36,320
domain controller the system has logged on to.

42
00:02:36,320 --> 00:02:40,640
In case of non‑domain joined machine or local user login,

43
00:02:40,640 --> 00:02:43,440
local computers name will be shown here.

44
00:02:43,440 --> 00:02:47,640
We then have some variables that gives information about processor.

45
00:02:47,640 --> 00:02:52,350
The OneDrive variable specifies the path to Users OneDrive location.

46
00:02:52,350 --> 00:02:55,520
The Path variable specifies group of directories where

47
00:02:55,520 --> 00:02:59,640
executable programs are located.

48
00:02:59,640 --> 00:03:04,740
PSModulePath specifies the paths where PowerShell modules resides.

49
00:03:04,740 --> 00:03:07,310
Then we have the variable for PUBLIC folder,

50
00:03:07,310 --> 00:03:11,640
SystemDrive, SystemRoot, and Temp folders.

51
00:03:11,640 --> 00:03:15,430
We then have variables that provides information about Users domain,

52
00:03:15,430 --> 00:03:19,040
Users account, and Users profile.

53
00:03:19,040 --> 00:03:19,600
Lastly,

54
00:03:19,600 --> 00:03:25,560
we have windir variable that specifies the drive where Windows is installed.

55
00:03:25,560 --> 00:03:29,400
I'll now show you how to extract values from these variables.

56
00:03:29,400 --> 00:03:32,670
The syntax to use environment variable is to start with

57
00:03:32,670 --> 00:03:37,180
dollar sign and then and enter env:colon,

58
00:03:37,180 --> 00:03:42,620
followed by variable name, First, I'll take an example of COMPUTERNAME variable.

59
00:03:42,620 --> 00:03:46,840
When I hit Enter, it will return COMPUTERNAME.

60
00:03:46,840 --> 00:03:50,410
The example which we saw earlier where COMPUTERNAME was served

61
00:03:50,410 --> 00:03:53,490
in the file can be achieved by doing this.

62
00:03:53,490 --> 00:03:59,240
In the FilePath parameter of Out‑File command, once you enter the FileName,

63
00:03:59,240 --> 00:04:01,940
you can append the COMPUTERNAME.

64
00:04:01,940 --> 00:04:04,940
This will extract hostname from COMPUTERNAME environment

65
00:04:04,940 --> 00:04:08,440
variable and append it to the FileName.

66
00:04:08,440 --> 00:04:12,480
I'll now show you the file that is created. Here,

67
00:04:12,480 --> 00:04:16,330
it has the FileName, followed by the hostname.

68
00:04:16,330 --> 00:04:17,240
Similarly,

69
00:04:17,240 --> 00:04:24,040
I can use USERPROFILE variable to save the file directly in the Users directory.

70
00:04:24,040 --> 00:04:28,320
I can fetch my Log‑on server using LOGONSERVER variable. I

71
00:04:28,320 --> 00:04:31,980
can also get the details of my Active Directory domain using

72
00:04:31,980 --> 00:04:35,110
USERDNSDOMAIN variable; likewise,

73
00:04:35,110 --> 00:04:43,000
you can make use of these env variables in a number of places, and that totally depends on your script requirement.

